gpt4 book ai didi

python - 在python中查找所有字符与其他单词匹配的单词

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:26 25 4
gpt4 key购买 nike

像 umbellar = umbrella 都是等同的词。

输入 = ["umbellar","goa","umbrella","ago","aery","alem","ayre","gnu","eyra","egma","game", "leam","amel","year","meal","yare","gun","alme","ung","male","lame","mela","mage"]

所以输出应该是:

输出=[ [“伞”,“伞”], [“前”,“果阿”], ["aery","ayre","eyra","yare","year"], ["alem","alme","amel","lame","leam","male","meal","mela"], [“gnu”,“枪”,“ung”] [“egma”,“游戏”,“法师”], ]

最佳答案


from itertools import groupby

def group_words(word_list):
sorted_words = sorted(word_list, key=sorted)
grouped_words = groupby(sorted_words, sorted)
for key, words in grouped_words:
group = list(words)
if len(group) > 1:
yield group

例子:

>>> group_words(["umbellar","goa","umbrella","ago","aery","alem","ayre","gnu","eyra","egma","game","leam","amel","year","meal","yare","gun","alme","ung","male","lame","mela","mage" ])
<generator object group_words at 0x0297B5F8>
>>> list(_)
[['umbellar', 'umbrella'], ['egma', 'game', 'mage'], ['alem', 'leam', 'amel', 'meal', 'alme', 'male', 'lame', 'mela'], ['aery', 'ayre', 'eyra', 'year', 'yare'], ['goa', 'ago'], ['gnu', 'gun', 'ung']]

关于python - 在python中查找所有字符与其他单词匹配的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4906673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com