Discuss / Python / 作业有个问题

作业有个问题

Topic source
L=['Hlleo','World',18,'Apple',None]
X=[s.lower() for s in L if isinstance(s, str)]
print(L)
print(X)

完成这个很简单,但是我对题目的理解是,把大写变小写,而其他不变。但是通过上面的代码就只能做到保留字符型,18,None都没了~~~ print结果为: [hello,world,apple] 而不是: [hello,world,18,apple,None]

然后想问,用列表生成器可以直接达到我想的那种结果吗?

好吧 我看到博主的期待输出了……眼拙了

zyz_hit

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

[d.lower() if isinstance(d,str) else d for d in L]


  • 1

Reply