开发者

using method as function in python

开发者 https://www.devze.com 2023-03-11 06:20 出处:网络
Say I have a list of strings, and I wan开发者_如何学编程t to filter out all non-upper case strings. Is there a simpler way than doing filter(lambda x: x.isupper(), list)?While I would prefer a list co

Say I have a list of strings, and I wan开发者_如何学编程t to filter out all non-upper case strings. Is there a simpler way than doing filter(lambda x: x.isupper(), list)?


While I would prefer a list comprehension, this seems to be what you're looking for:

filter(str.isupper, list)


uppers = [s for s in list if s.isupper()]
0

精彩评论

暂无评论...
验证码 换一张
取 消