Python - 匹配列表中单词的字母

标签 python

我正在尝试创建一个用户输入几个字母的简单程序

输入字母:abc

然后我想遍历 list 和 match 中的单词列表以及包含“a”、“b”和“c”的单词。

这是我到目前为止尝试过的方法,但没有成功

for word in good_words: #For all words in good words list
    for letter in letters: #for each letter inputed by user
        if not(letter in word):
            break
    matches.append(word)

最佳答案

如果你想要所有单词中的字母:

[word for word in good_words if all(letter in word for letter in letters)]

您的代码的问题在于内部循环中的 break。 Python 没有允许一次打破多个循环的构造(而你想要那样)

关于Python - 匹配列表中单词的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11426314/

相关文章:

python - 如何在 python pandas 中转换时间列并查找具有条件的时间增量

python - 我的 Python 版本的玩具 SVM 出了什么问题?

python - 如何将数组存储在太大而无法加载到内存中的 hdf5 文件中?

python - 如何在更改变量时触发 if 语句 (Python3)

python - Python 中的 if 和 else 条件问题

python - 如何在 OS X 上的 pythonbrew 下构建 pgmagick?

Python:使用 setup.py install (distutil) 时如何强制覆盖文件

python - 为什么 zlib.crc32 比 binascii.crc32 快?

使用 numba 的 Python 代码计时

python - GitBash 中的错误 virtualenvwrapper