过滤包含特定ord中某些字母的单词

2024-10-06 12:08:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,我想搜索一个字典,比如Scrabble官方列表,并按照特定的顺序识别具有x个字符数的所有单词。例如,我有“mmt”,并希望输出生成一个单词列表,如下面所示。在

“mmt”:

  • AM藻类MATED
  • AMMONIAT编辑
  • 循环MAMBULATED
  • COMMENT日期

谢谢你!!在


Tags: 编辑列表字典官方顺序commentam单词
1条回答
网友
1楼 · 发布于 2024-10-06 12:08:37

您可以生成一个动态正则表达式模式并基于该模式筛选列表:

import re

words = ["AMALGAMATED", "AMMONIATED", "CIRCUMAMBULATED", "COMMENTATED", 
         "TAMTAM", "BLUB", "HOUSE", "SOMETHING"]
filter = "mmt"

regex = re.compile(".*".join(filter), re.IGNORECASE)
filtered_words = [word for word in words if regex.search(word)]

print(*filtered_words, sep="\n")

See this code running on ideone.com

相关问题 更多 >