列表生成式.py
Topic sourceL1 = ['Hello', 'World', 18, 'Apple', None] L2 = [s.lower() for s in L1 if isinstance(s, str)] print(L2)
['hello', 'world', 'apple'] 是对的吧,我这边运行结果是这个
非字符串的你没打印出来啊 所以不对 1)[x.lower() if isinstance(x,str) else x for x in L ] 2) [x.lower() if isinstance(x,str) else x if isinstance(x,int) else None for x in L]
- 1
Tedz-
L1 = ['Hello', 'World', 18, 'Apple', None] L2 = [s.lower() for s in L1 if isinstance(s,str)] print(L2)