LoginSignup
1
1

More than 5 years have passed since last update.

collections.Counterを用いてlistのカウントアップ

Last updated at Posted at 2014-01-08
a = ["poko", "poko", "hoge", "aaa", "aaa", "poko", "aaa", "aab"]

のようなlistがある

from collections import Counter
counts = Counter(a)
#上位2番目
print counts.most_common(2)
# [Out]: [('poko', 3), ('aaa', 3)]

#上位から全部
print counts.most_common()
# [Out]: [('poko', 3), ('aaa', 3), ('aab', 1), ('hoge', 1)]

# key指定
print counts[“poko”]
# [Out]: 3

sort済みのdict(みたいな言い方で良いのか?)っぽくつかえる

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1