Counter()のアプリケーションデモンストレーション:
from collections import Counter
list_01 = ['A','C','S','A','B','f','S','A']
dict_01 = Counter(list_01)
print(dict_01)
辞書を直接出力します。内容はリスト内の要素とその出現頻度です。
Counter({
'A': 3, 'S': 2, 'C': 1, 'B': 1, 'f': 1})
さらに、Counter()をインスタンス化できます。
total_counts = Counter()
print(type(total_counts))
total_countsのタイプは、クラスとして表示されます。
<class 'collections.Counter'>
インスタンス化後、次の形式でさらにトラバースするために使用できます。
for word,freq in total_counts:
if freq>1:
print(word)