python - 存储 "if any("搜索的值

标签 python python-2.7

我正在尝试存储单词的值,有办法做到这一点吗?

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/

相关文章:

python - 将 QTableWidget 单元格链接到网页

python - Pandas 通过滚动打开的窗口进行分组

python - 如果 Pandas 从多列返回值等于另一列中的值

python-2.7 - 将 diff 与漂亮的 soup 对象一起使用

python - 将多个数组组合成一个分箱数组(性能)

python - 在 Python 中提取列表元素

python - socket ? python -m SimpleHTTPServer

python - python字典中的求和

python-2.7 - 在python中使用sftp从远程路径获取文件到本地目录

python将多个子图图形保存为pdf