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. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?2. 在windows下安裝docker Toolbox 啟動Docker Quickstart Terminal 失敗!3. docker-compose中volumes的問題4. docker不顯示端口映射呢?5. golang - 用IDE看docker源碼時的小問題6. docker內創建jenkins訪問另一個容器下的服務器問題7. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””8. javascript - 最近用echarts做統計圖時遇到兩個問題!!9. javascript - 連續點擊觸發mouseleave事件10. mac里的docker如何命令行開啟呢?

網公網安備