#_*_ coding:utf-8 _*_
L1 = ['Hello', 'World', 18, 'Apple', None]
L2=[s.lower() for s in L1 if isinstance(s,str)]
L2
[40]:
['hello', 'world', 'apple']
[43]:
L2=[s.lower() if isinstance(s,str) else s for s in L1]
['hello', 'world', 18, 'apple', None]
L2 = [s.lower() for s in L1 if isinstance(s,str) else s]
这个为什么不行呢
同问
上面文章写了,在一个列表生成式中,for前面的if ... else是表达式,而for后面的if是过滤条件,不能带else。
for
if ... else
if
else
Sign in to make a reply
这样的话17422
[40]:
[43]:
[43]: