L1 = ['Hello', 'World', 18, 'Apple', None] L2=[]
[L2.append(s.lower()) if isinstance(s,str) else L2.append(s) for s in L1 ]
print(L2)
结果 ['hello', 'world', 18, 'apple', None]
Sign in to make a reply
用户4403728317
L1 = ['Hello', 'World', 18, 'Apple', None] L2=[]
[L2.append(s.lower()) if isinstance(s,str) else L2.append(s) for s in L1 ]
加了一个三元表达式,一条代码解决迭代,判断,写入
print(L2)
结果 ['hello', 'world', 18, 'apple', None]