• 列表元素的几种统计方法总结(嵌套列表)


    (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]) +1
        else:
            dict[i] = 1
    

    (3)Counter(速度快)

    from collections import 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
    #对字典排序(倒序),变为值由大到小的列表
    cnt_data=sorted(cnt.items(),key=lambda x:x[1],reverse=True)

      

      

  • 相关阅读:
    linux文件系统
    用户态和内核态
    nginx优化
    平滑升级nginx
    网络--基本概念
    haproxy
    awk
    kvm
    lvs
    自定义不等高cell—storyBoard或xib自定义不等高cell
  • 原文地址:https://www.cnblogs.com/lzq116/p/10592550.html
Copyright © 2020-2023  润新知