`
scm002
  • 浏览: 309295 次
社区版块
存档分类
最新评论

思考:如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错:

 
阅读更多
>>> L = ['Hello', 'World', 18, 'Apple', None]
>>> [s.lower() for s in L]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'lower

t = ['Hello', 'World', 18, 'Apple', None]
print [s.lower() if isinstance(s, str) else s for s in t]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics