zoukankan      html  css  js  c++  java
  • 一道面试题:统计重复并排序

    题目

    给定列表,列表内的数据全是str类型,对列表内的数据进行统计元素出现的次数,并排序。

    例:["a", "b", "g", "g", "b", "g", "l", "k", "k"]

    返回:{'g': 3, 'b': 2, 'k': 2, 'a': 1, 'l': 1}

    解1

    不使用任何内置函数:

    def count_list_1(data:list):
        res = {}
        for item in data:
            if item not in res.keys():
                res[item] = 1
            else:
                res[item] += 1
        return dict(sorted(res.items(), key=lambda i:i[1], reverse=True))
    

    测试:

    print(count_list_1(["a", "b", "g", "g", "b", "g", "l", "k", "k"]))
    
    # 返回
    {'g': 3, 'b': 2, 'k': 2, 'a': 1, 'l': 1}
    

    解2

    使用列表的count函数。

    def count_list_2(data:list):
        res = {}
        for item in data:
            res[item] = data.count(item)
        return dict(sorted(res.items(), key=lambda i:i[1], reverse=True))
    

    测试:

    print(count_list_2(["a", "b", "g", "g", "b", "g", "l", "k", "k"]))
    
    # 返回
    {'g': 3, 'b': 2, 'k': 2, 'a': 1, 'l': 1}
    

    解3

    使用collections.Counter

    import collections
    
    def count_list_3(data:list):
        return dict(sorted(collections.Counter(data).items(), key=lambda i: i[1], reverse=True))
    

    测试:

    print(count_list_3(["a", "b", "g", "g", "b", "g", "l", "k", "k"]))
    
    # 返回
    {'g': 3, 'b': 2, 'k': 2, 'a': 1, 'l': 1}
    

    消耗时间对比

    对代码进行改造,随机生成一个字符串列表。然后使用cProfile进行测试。

    # 统计列表中的重复的元素的个数, 并对结果进行排序
    # ["a", "b", "g", "g", "b", "g"] => {"a": 1, "b":2, "g": 3}
    import collections
    import random
    import string
    
    
    def count_list_1(data: list):
        res = {}
        for item in data:
            if item not in res.keys():
                res[item] = 1
            else:
                res[item] += 1
        return dict(sorted(res.items(), key=lambda i: i[1], reverse=True))
    
    
    def count_list_2(data: list):
        res = {}
        for item in data:
            if item not in res.keys():
                res[item] = data.count(item)
        return dict(sorted(res.items(), key=lambda i: i[1], reverse=True))
    
    
    def count_list_3(data: list):
        return dict(sorted(collections.Counter(data).items(), key=lambda i: i[1], reverse=True))
    
    
    if __name__ == "__main__":
        data = [random.choice(string.ascii_letters) for i in range(10000)]
        count_list_1(data)
        count_list_2(data)
        count_list_3(data)
    
    

    测试命令: python -m cProfile -s tottime test.py |grep count_list

    列表长度为10000,测试结果:

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      1    0.002    0.002    0.003    0.003 test.py:9(count_list_1)
      1    0.001    0.001    0.010    0.010 test.py:19(count_list_2)
      1    0.000    0.000    0.000    0.000 test.py:27(count_list_3)
    

    列表长度为50000,测试结果:

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.010    0.010    0.013    0.013 test.py:9(count_list_1)
        1    0.006    0.006    0.044    0.044 test.py:19(count_list_2)
        1    0.000    0.000    0.002    0.002 test.py:27(count_list_3)
    
    

    列表长度为100000,测试结果:

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.020    0.020    0.026    0.026 test.py:9(count_list_1)
        1    0.012    0.012    0.090    0.090 test.py:19(count_list_2)
        1    0.000    0.000    0.004    0.004 test.py:27(count_list_3)
    
    

    由此可见,collections.Counter性能是最好的, 列表中的count函数次之。

    有兴趣的童鞋可以研究下Counter的源码是怎么写的。

    Enjoy your code, good luck.

    不积跬步,无以至千里。
  • 相关阅读:
    mysql5.7.10 源码编译安装记录 (centos6.4)【转】
    bash脚本里su命令执行
    linux服务器登录时慢出现卡顿
    iptables NAT规则【转】
    双机/RAC/Dataguard的区别【转】
    一步一步搭建 oracle 11gR2 rac + dg 之前传 (一)【转】
    一步一步搭建oracle 11gR2 rac+dg之环境准备(二)【转】
    一步一步搭建oracle 11gR2 rac+dg之共享磁盘设置(三)【转】
    一步一步搭建 oracle 11gR2 rac+dg之grid安装(四)【转】
    一步一步搭建oracle 11gR2 rac+dg之database安装(五)【转】
  • 原文地址:https://www.cnblogs.com/DeaconOne/p/12724822.html
Copyright © 2011-2022 走看看