繁体   English   中英

蟒蛇3。 如何检查后缀是否在列表中的某些单词中?

[英]Python3. How to check if a suffix is in some of the words in a list?

我正在使用 python3 并遇到这个问题。 说我有一个清单

L = ["money", "fame", "popularity"]

我想检查L中是否有包含"pop"单词。 我知道如果L是一个字符串,例如如果L = "I want to be popular" ,那么我可以"pop" in l执行"pop" in l ,并且将返回布尔值。 但是现在L是一个列表,我如何检查这个列表中是否有任何单词包含"pop"呢? (如果可能,不使用循环)

使用any()为任何匹配案例返回True

any('pop' in x for x in L)

代码

L = ["money", "fame", "popularity"]

print(any('pop' in x for x in L))
# True

您可以将过滤器与 lambda 函数一起使用

L = ["populate", "position", "popcorn"]
didContainPop = len(list(filter(lambda x: "pop" in x, L))) != 0
print(didContainPop)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM