python - 如何對列表中的列表進行頻率統計?
問題描述
例如此列表:
[[’software’, ’foundation’], [’of’, ’the’], [’the’, ’python’], [’software’, ’foundation’],[’of’, ’the’], [’software’, ’foundation’]]# 進行頻率統計,例如輸出結果為:('[’software’,’foundation’]', 3), ('[’of’, ’the’]', 2), ('[’the’, ’python’]', 1)
問題解答
回答1:# coding:utf8from collections import Countera = [[’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() # 以列表形式返回統計結果回答2:
from collections import Counterdata = [[’software’, ’foundation’], [’of’, ’the’], [’the’, ’python’], [’software’, ’foundation’],[’of’, ’the’], [’software’, ’foundation’]]cnt = Counter(map(tuple, data))print(list(cnt.items()))回答3:
from itertools import groupbydata = ....print [(k, len(list(g)))for k, g in groupby(sorted(data))]
相關文章:
1. docker網絡端口映射,沒有方便點的操作方法么?2. nignx - docker內nginx 80端口被占用3. angular.js - angular內容過長展開收起效果4. javascript - canvas 裁剪空白區域5. docker不顯示端口映射呢?6. docker綁定了nginx端口 外部訪問不到7. javascript - nodejs調用qiniu的第三方資源抓取,返回401 bad token,為什么8. docker api 開發的端口怎么獲取?9. debian - docker依賴的aufs-tools源碼哪里可以找到啊?10. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?
