![見出し画像](https://assets.st-note.com/production/uploads/images/30669071/rectangle_large_type_2_72a926896d74f186de8a786719e5333a.png?width=1200)
pythonの小技集-Counterで出現回数を数える-
コードその1
from collections import Counter
fruits = ['りんご', 'みかん', 'バナナ', 'りんご', 'みかん', 'バナナ']
fruits_count = Counter(fruits)
print(fruits_count)
出力結果その1
Counter({'りんご': 2, 'みかん': 2, 'バナナ': 2})
コードその2
from collections import Counter
fruits = ['りんご', 'みかん', 'バナナ', 'りんご', 'みかん', 'バナナ']
fruits_count = Counter(fruits)
fruits_count = fruits_count['りんご']
print(fruits_count)
出現結果その2
2
この記事が気に入ったらサポートをしてみませんか?