gpt4 book ai didi

python - 脚本多次返回相同的值

转载 作者:行者123 更新时间:2023-11-28 17:50:51 26 4
gpt4 key购买 nike

<分区>

基本上我有一个 Python 脚本,它接受几个字母,获取它们的每个组合,然后检查它是否是一个实际的单词(从某种意义上说,想想拼字游戏),但出于某种原因,它多次返回相同的单词,我不想让它这样做,脚本如下所示:

with open("dictionary.txt") as word_file:
english_words = set(word.strip().lower() for word in word_file)

def is_english_word(word):
return word.lower() in english_words

print is_english_word("ham")
print is_english_word("zz")

a = raw_input("Please enter first letter: ")
b = raw_input("Please enter second letter: ")
c = raw_input("Please enter third letter: ")
d = raw_input("Please enter fourth letter: ")
e = raw_input("Please enter fifth letter: ")

check =[a,b,c,d,e]

def get_combos(list):
import itertools
count = len(list)
got = []
combos =[]
while count > 0:
for a in itertools.permutations(list,count):
if a in got:
got.append(a)
else:
got.append(a)
combos.append(a)
count = count - 1
for a in combos:
strip_combos(a)

def strip_combos(list):
count = ''
words = []
for entry in list:
count = count + entry
words.append(count)
check_combo(words)

def check_combo(list):
words = []
got = []
for entry in list:
if is_english_word(entry):
if entry not in words:
print entry
words.append(entry)

get_combos(check)

现在它也按照我的意思工作,只打印字典中的单词,但它会多次打印相同的单词,例如,如果字母是:

a, c, e, s

它会在每次出现在列表中时返回,尽管据我所知我忽略了在 check_combo 过程中多次出现的相同结果,方法是有一个 got 和一个单词列表

我感觉问题可能源于某处 while 循环中的 get_combos 过程,尽管我尝试修改几乎所有内容都无济于事,所以我转向那些比我更有知识的人寻求帮助。

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