我正在尝试存储单词的值,有办法做到这一点吗?
if any(word in currentFile for word in otherFile):
最佳答案
如果您想要单词本身,请不要使用any
:
words = [word for word in otherFile if word in currentFile]
然后你可以直接进行真值测试(因为空列表是假的):
if words:
# do stuff
并且还可以访问匹配的单词:
print words
编辑:如果您只想要第一个匹配单词,您也可以这样做:
word = next((word for word in otherFile if word in currentFile), None)
if word:
# do stuff with word
关于python - 存储 "if any("搜索的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493518/