l = ['Hello', 'World', 18, 'Apple', None]
L = [x.lower() for x in l if isinstance(x,str)]#数字和none会不见,不是最好方法
#下面的方法可以保留
L = [x.lower() if isinstance(x, str) else x for x in l]#最好的方法,保留数字和none
if __name__ == '__main__':
print(L)
Python——列表生成式备忘
最新推荐文章于 2021-11-02 22:43:12 发布