Pythonベース:Python統計リストに単語ごとに出現する回数(splitの使用,for二重ループ)
7608 ワード
Pythonで複数の文字列を含むリスト内の単語の出現回数を統計するにはどうすればいいですか?
複数の文字の中から回数を統計するには、まず簡単な例を見てみましょう.
まず、1つの文字列から各単語の出現回数を統計します.
文字列:
str=“You may be out of my sight, but never out of my mind.”
論理を簡略化するために、文字列を以下のように処理します.
str=「You may be out of my sight but never out of my mind」(すべての句読点をスペースに変更)
1.まず、
ここではまず初心者にPythonでよく使われる方法を紹介します:split()構文:
str.split(str="", num=string.count(str)).
パラメータ:
str------スペース、改行()、タブ(t)など、デフォルトではすべての空の文字で区切ります.num-----分割回数.デフォルトは-1です.つまり、すべてを区切ります.(回数無制限)
戻り値:
分割された文字列のリストを返します.
注意:Pythonのsplit()メソッドは、区切り記号を指定して文字列をスライスし、パラメータnumに指定値がある場合はnum+1サブ文字列を区切ります.
例:
実行結果:
[‘Whatever’, ‘is’, ‘worth’, ‘doing’, ‘is’, ‘worth’, ‘doing’, ‘well’] [‘Whatever’, ‘is worth doing is worth doing well’] [‘Whatever’, ‘is’, ‘worth doing is worth doing well’] #…
2.続行:
ここで、重要な方法を理解しました.最初の問題は、文字列ごとに単語が現れる回数を統計することです.コードは次のとおりです.
実行結果:
{‘You’: 1, ‘may’: 1, ‘be’: 1, ‘out’: 2, ‘of’: 2, ‘my’: 2, ‘sight’: 1, ‘but’: 1, ‘never’: 1, ‘mind.’: 1}
3.最後:
最初の質問に戻ります.
Pythonで複数の文字列を含むリスト内の単語の出現回数を統計するにはどうすればいいですか?
リストに複数の文字列が含まれている場合、forの二重ループを使用してリスト全体を巡回します.コードは次のとおりです.
実行結果:
{‘hello’: 2, ‘you’: 3, ‘my’: 1, ‘friends’: 1, ‘are’: 2, ‘so’: 1, ‘beautiful’: 2, ‘a’: 2, ‘girl’: 1, ‘good’: 1, ‘boy’: 1}
複数の文字の中から回数を統計するには、まず簡単な例を見てみましょう.
まず、1つの文字列から各単語の出現回数を統計します.
文字列:
str=“You may be out of my sight, but never out of my mind.”
論理を簡略化するために、文字列を以下のように処理します.
str=「You may be out of my sight but never out of my mind」(すべての句読点をスペースに変更)
1.まず、
ここではまず初心者にPythonでよく使われる方法を紹介します:split()構文:
str.split(str="", num=string.count(str)).
パラメータ:
str------スペース、改行()、タブ(t)など、デフォルトではすべての空の文字で区切ります.num-----分割回数.デフォルトは-1です.つまり、すべてを区切ります.(回数無制限)
戻り値:
分割された文字列のリストを返します.
注意:Pythonのsplit()メソッドは、区切り記号を指定して文字列をスライスし、パラメータnumに指定値がある場合はnum+1サブ文字列を区切ります.
例:
str = "Whatever is worth doing is worth doing well" #
print(str.split()) # ,
print(str.split(' ', 1 )) # , ( )
print(str.split(' ', 2 )) # , ( )
#......
実行結果:
[‘Whatever’, ‘is’, ‘worth’, ‘doing’, ‘is’, ‘worth’, ‘doing’, ‘well’] [‘Whatever’, ‘is worth doing is worth doing well’] [‘Whatever’, ‘is’, ‘worth doing is worth doing well’] #…
2.続行:
ここで、重要な方法を理解しました.最初の問題は、文字列ごとに単語が現れる回数を統計することです.コードは次のとおりです.
List=["You may be out of my sight but never out of my mind."] #
# ,
dict={} # ,
keys=List[0].split() #
for key in keys: #
if key in dict.keys(): # key
dict[key]=dict[key]+1 # key 1
else:
dict[key]=1 # key
print(dict)
実行結果:
{‘You’: 1, ‘may’: 1, ‘be’: 1, ‘out’: 2, ‘of’: 2, ‘my’: 2, ‘sight’: 1, ‘but’: 1, ‘never’: 1, ‘mind.’: 1}
3.最後:
最初の質問に戻ります.
Pythonで複数の文字列を含むリスト内の単語の出現回数を統計するにはどうすればいいですか?
リストに複数の文字列が含まれている場合、forの二重ループを使用してリスト全体を巡回します.コードは次のとおりです.
List=["hello you","hello my friends","you are so beautiful","you are a beautiful girl","a good boy"]
dict={}
for list in List:
keys=list.split(" ")
for key in keys:
if key in dict.keys():
dict[key]=dict[key]+1
else:
dict[key]=1
print(dict)
実行結果:
{‘hello’: 2, ‘you’: 3, ‘my’: 1, ‘friends’: 1, ‘are’: 2, ‘so’: 1, ‘beautiful’: 2, ‘a’: 2, ‘girl’: 1, ‘good’: 1, ‘boy’: 1}