python - Bagaimana untuk melakukan statistik kekerapan pada senarai dalam senarai?
仅有的幸福
仅有的幸福 2017-05-18 11:02:05
0
3
594

Sebagai contoh senarai ini:

[['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]


# 进行频率统计,例如输出结果为:
("['software','foundation']", 3), ("['of', 'the']", 2), ("['the', 'python']", 1)
仅有的幸福
仅有的幸福

membalas semua(3)
漂亮男人
# coding:utf8
from collections import Counter
a = [['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]
print Counter(str(i) for i in a)   # 以字典形式返回统计结果
print Counter(str(i) for i in a).items()  # 以列表形式返回统计结果

# -------------- map方法 --------
print Counter(map(str, a))   # 以字典形式返回统计结果
print Counter(map(str, a)).items()  # 以列表形式返回统计结果
伊谢尔伦
from collections import Counter
data = [['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]
cnt = Counter(map(tuple, data))
print(list(cnt.items()))
習慣沉默
from itertools import groupby
data = ....
print [(k, len(list(g)))for k, g in groupby(sorted(data))]
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!