Discuss / Python / 列表生成式.py

列表生成式.py

Topic source

Tedz-

#1 Created at ... [Delete] [Delete and Lock User]

L1 = ['Hello', 'World', 18, 'Apple', None] L2 = [s.lower() for s in L1 if isinstance(s,str)] print(L2)

风为裳J

#2 Created at ... [Delete] [Delete and Lock User]

错了

运用列表生成式,可以快速生成list,可以通过一个list推导出另一个list,而代码却十分简洁

紫桓殿下

#4 Created at ... [Delete] [Delete and Lock User]

L1 = ['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]

lin金妮

#6 Created at ... [Delete] [Delete and Lock User]

说错的大概是审题不严谨吧, 题目下方期待输出是['hello','world','apple']


  • 1

Reply