0
一個文件, 127,000+字被導入並列入列表Python:For循環在調用時減半我的列表
try:
dictionary = open("dictionary.txt", "r")
except:
print("Dictionary not found")
exit()
list_of_words = [word.rstrip('\n').rstrip('\r') for word in dictionary]
當用戶輸入字長時,它會檢查以確保該單詞在參數內。
def length_check(x):
while(1):
x = int(input("Please enter a word length: "))
if x >= 1 and x <=147:
return
else:
print ('enter proper length')
,然後又把這個詞的長度並檢查它反對在列表中的單詞,並刪除列表中的任何字,那不是等於「word_length」
def read_and_delete(x):
i = 0
for removal in x:
if len(x[i]) != word_length:
del x[i]
i += 1
elif len(x[i]) == word_length:
i += 1
else:
continue
print(len(list_of_words))
但由於某些原因,輸出的結果恰好是列表中的一半,我不明白爲什麼代碼中沒有任何分隔。
您在迭代它時將從列表中刪除項目。 –