這個程式的功能就像一個字謎,下面的部分顯示了一個小演算法,它遍歷一個給定單詞的串列,這些單詞存盤在一個名為的串列中word_list,并將其中的專案choice與用戶插入的單詞進行比較。
第一個回圈遍歷串列中的每一個專案,并將它們分配給word然后將shared_letters(決定是否word可以在其中找到字母的計數器choice)設定為零,然后開始遍歷兩個單詞之間的共享字母,以免i在第二個回圈期間溢位可迭代。
第二個回圈使用 存盤在其中x的長度進行迭代。然后回圈執行一個條件 if 陳述句,該陳述句決定 ( ) 的索引字母是否在( )中找到。如果是,則計數器 增加 1,否則它會跳出第二個回圈并回傳第一個回圈以獲取新單詞。wordword lengthxsliced wordlist(word)sliced choicelist(choice)shared_letters
回圈程序在我之前運行良好,但由于某些原因,在這段代碼中它根本不再運行第二個回圈,我嘗試放入print 陳述句來檢查程式所采用的路線,而且它總是跳過了嵌套的 for 回圈。即使我嘗試將它變成類似函式的東西,程式也只是拒絕執行該函式。
choice = input("Enter a word: ") # User enters a word
# Algorithm below compares the entered word with all the words found in the dictionary, then saves any words found into "sushi" list
for i in range(num_words): # Word loop, gives iterated word
word = word_list[i] # Picks a loop
shared_letters = 0 # Resets # of shared letters
for x in range(word_length): # Goes through the letters of iterated word
if sliced_word[x] in sliced_choice:
shared_letters = x 1
elif sliced_word[x] not in sliced_choice:
break
這是完整的程式,以防您想更好地了解它,對不起,如果編碼看起來很混亂,我一直在嘗試使用這個程式,但我似乎永遠無法找到一個好的解決方案。
word_list = ["race","care","car","rake","caring","scar"]
sushi = []
word = ""
sliced_word = list(word)
word_length = len(sliced_word)
choice_word = ""
sliced_choice = list(choice_word)
choice_length = len(sliced_choice)
shared_letters = 0
num_words = len(word_list)
next_word = False
choice = input("Enter a word: ") # User enters a word
# Algorithm below compares the entered word with all the words found in the dicitionary, then saves any words found into "sushi" list
for i in range(num_words): # Word loop, gives iterated word
word = word_list[i] # Picks a loop
shared_letters = 0 # Resets # of shared letters
for x in range(word_length): # Goes through the letters of iterated word
if sliced_word[x] in sliced_choice:
# Checks if the letters of the iterated word can be found in the choice word
shared_letters = x 1
elif sliced_word[x] not in sliced_choice:
break # If any of the letters within the iterated word are not within the choice word, it moves onto the next word
if shared_letters == word_length:
sushi.append(word_list[i])
# If all of the letters within the iterated word are found in the choice word, it appends the iterated word into the "sushi" list. Then moves onto the next word in the word_list.
uj5u.com熱心網友回復:
你有很多問題,但我認為最大的問題是這個搜索沒有考慮到具有多個相同字母的字謎。確定一個單詞是否是字謎的最簡單方法是查看它們每個字母的計數是否相同。
有一個Counter從collections模塊呼叫的內置幫助器類可以幫助解決這個問題。
>>> from collections import Counter
>>> Counter("care")
Counter({'c': 1, 'a': 1, 'r': 1, 'e': 1})
>>> Counter("race")
Counter({'r': 1, 'a': 1, 'c': 1, 'e': 1})
>>> Counter("care") == Counter("race")
True
將此應用于您的示例,您可以像這樣重構:
word_list = ["race","care","car","rake","caring","scar"]
sushi = []
for word in word_list:
if Counter(choice) == Counter(word):
sushi.append(word)
現在,如果我們必須一遍又一遍地制作 Counter 物件,這有點慢,所以您可以選擇將它們存盤在字典中:
word_list = ["race","care","car","rake","caring","scar"]
word_counters = {word: Counter(word) for word in word_list}
sushi = []
for word, counter in word_counters.items():
if Counter(choice) == counter:
sushi.append(word)
如果你想找到一個不完美的匹配,假設一個單詞包含在另一個單詞中,你可以使用-運算子并測驗左側是否有任何字母留下:
>>> not (Counter("race") - Counter("racecar"))
True
>>> not (Counter("race") - Counter("bob"))
False
將其回傳到示例中:
word_list = ["race","care","car","rake","caring","scar"]
word_counters = {word: Counter(word) for word in word_list}
sushi = []
for word, counter in word_counters.items():
if not (Counter(choice) - counter):
sushi.append(word)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452830.html
