python学习笔记:列表生成器
提问:请修改列表生成式,通过添加if语句保证列表生成式能正确地执行:
L1 = [‘Hello’, ‘World’, 18, ‘Apple’, None]
回答:L1=[‘Hello’, ‘World’, 18, ‘Apple’, None]
L2=[]
for i in L1:
if isinstance(i,str)==True:
L2.append(i)
L2=[s.lower() for s in L2]
python学习笔记:列表生成器
提问:请修改列表生成式,通过添加if语句保证列表生成式能正确地执行:
L1 = [‘Hello’, ‘World’, 18, ‘Apple’, None]
回答:L1=[‘Hello’, ‘World’, 18, ‘Apple’, None]
L2=[]
for i in L1:
if isinstance(i,str)==True:
L2.append(i)
L2=[s.lower() for s in L2]