Discuss / Python / 有点长,用了个三元表达式简化了一下

有点长,用了个三元表达式简化了一下

Topic source

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]


  • 1

Reply