python - 如何打印包含特定字母的单词
问题描述
我有单词文件,每行包含一个单词。我尝试做的是向用户询问字母并搜索用户输入的所有这些字母的单词。我工作了几天,但无法使第 7 行和第 8 行正常运行,只会出现不同的错误,或者没有给出任何结果。
letters = input('letters: ')
words = open('thesewords').read().splitlines()
print (words)
print(".......................")
for word in words:
if all(letters) in word:
print(word)
解决方案
你用all()
错了。
all(letters)
始终是True
for string letters
,并True in <string>
返回一个TypeError
.
你应该做的是:
all(x in word for x in letters)
所以,它变成:
for word in words:
if all(x in word for x in letters):
print(word)
推荐阅读
- c++ - QMimeData::data() 函数调用需要太长时间才能完成
- javascript - 如何使用 javascript 或 css 获取手机的虚拟键盘高度?
- javascript - DataTable 未应用语言指令
- r - 如何在 r 中创建小标题?
- c++ - 复制和移动构造函数
- sql - 在 oracle 中插入和返回值
- kubernetes-ingress - LoadBalancer X-Forwared-For 路由背后的入口
- python - Python打印状态行:最后打印的孤立字符
- r - 子集分组数据
- javascript - 从 wp 网站提取属性并使用 js 在新网站上查看它们