の修正とPythonの:は、forループ私はこれの.txtファイルを持っているiterationlist
king james version of the bible
the first book of moses called genesis
私は.txtファイルの統計のためのpythonスクリプトを使用するには、ファイルがリストに読み込まれfinal_list
、その後、I (長いスクリプトの一部)は、このコードを実行します。
for word in final_list:
output_list.append((word,final_list.count(word)))
final_list[:] = [x for x in final_list if x != word]
#DEBUGGING
print(len(final_list))
print(final_list)
:
12
['james', 'version', 'of', 'the', 'bible', 'the', 'first', 'book', 'of', 'moses', 'called', 'genesis']
11
['james', 'of', 'the', 'bible', 'the', 'first', 'book', 'of', 'moses', 'called', 'genesis']
9
['james', 'of', 'bible', 'first', 'book', 'of', 'moses', 'called', 'genesis']
8
['james', 'of', 'bible', 'book', 'of', 'moses', 'called', 'genesis']
6
['james', 'bible', 'book', 'moses', 'called', 'genesis']
5
['james', 'bible', 'book', 'moses', 'called']
これは私のpython for-loopが実際にどのように動作するのか不思議です。
あなたのコードは単語頻度を非常に非効率的にカウントします。 'collections.Counter()'を見てください。これはあなたの仕事をより簡単かつ迅速に行います。 –
ありがとう、私はそれを調べます。それでも、これは反復されていない最後の5単語の問題ではないと思いますよね? –
反復処理中のリストを変更しないでください。この動作は未定義です。 – Daniel