列表元素的几种统计方法总结(嵌套列表)
生活随笔
收集整理的這篇文章主要介紹了
列表元素的几种统计方法总结(嵌套列表)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(1)列表中的count方法(速度慢)
#嵌套列表類型的統計 l = [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]] dictionary= {} s = set(l) for i in s:dict[i] = l.count(i)(2)字典(速度慢)
l = [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]] dict = {} for i in l:if i in dict.keys():dict[i] = int(dict[i]) +1else:dict[i] = 1(3)Counter(速度快)
l = [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]]cnt = Counter() for i in l:i = tuple(i)cnt[i] += 1
轉載于:https://www.cnblogs.com/lzq116/p/10592550.html
總結
以上是生活随笔為你收集整理的列表元素的几种统计方法总结(嵌套列表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: unity检测范围内敌人_Unity实现
- 下一篇: 推荐几个学习GIS的社区