L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [s.lower() for s in L1 if isinstance(s, str)]
L2为最终结果的 列表解析表达式 s.lower() 最终需要生成的结果并转化成小写 for s in L1 数据来源 if isinstance(s, str) 判断,如果条件成立,才写入本列表,如果不成立就pass掉
s.lower()
for s in L1
if isinstance(s, str)
所以最终 L2的结果为 ['hello', 'world', 'apple']
['hello', 'world', 'apple']
Sign in to make a reply
ynxiu
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [s.lower() for s in L1 if isinstance(s, str)]
L2为最终结果的 列表解析表达式
s.lower()
最终需要生成的结果并转化成小写for s in L1
数据来源if isinstance(s, str)
判断,如果条件成立,才写入本列表,如果不成立就pass掉所以最终 L2的结果为
['hello', 'world', 'apple']