zoukankan      html  css  js  c++  java
  • 字典练习

    1、打印用户输入的数字,打印每一位数字,及其重复的次数

    num = '   0231324443510  '
    counter = {}
    num = num.strip(' ').rstrip('0')
    for i in num:
        if i not in counter.keys():
            counter[i] = 0
        counter[i] += 1
        
    print(counter)

    2、数字重复的统计,随机产生100个整数,范围在[-1000,1000],升序打印这些数字和其重复次数

    使用sorted函数排序,字典的缺省值添加使用d.get(i,0)或者d.setdefault(i,0)

    import random
    nums = [random.randint(-1000,1000) for _ in range(100)]
    counter={}
    for i in nums:
        counter[i] = counter.get(i,0) + 1
    newdict = sorted(counter.items())
    print(newdict)

    也可以使用工厂化函数创建缺省字典,from collections import defaultdict

    import random
    from collections import defaultdict
    nums = [random.randint(-1000,1000) for _ in range(100)]
    counter=defaultdict(int)
    for i in nums: counter[i] = counter[i] + 1
    newdict = sorted(counter.items()) print(newdict)

    使用列表排序后输出

    import random
    from collections import defaultdict
    nums = [random.randint(-1000,1000) for _ in range(100)]
    counter=defaultdict(int)
    for i in nums:
        counter[i] = counter[i] + 1
    
    key = list(counter.keys())
    key.sort()
    newlist = [0] * len(key)
    for j,val in enumerate(key):
        newlist[j] = val,counter[val]
    print(newlist)

    比较使用列表和使用字典的效率

    import random
    from collections import defaultdict
    nums = [random.randint(-1000,1000) for _ in range(100)]
    counter=defaultdict(int)
    
    for i in nums:
        counter[i] = counter[i] + 1

    先生成字典

    %%timeit
    newdict = sorted(counter.items())

    直接使用sorted 输出  21.4 µs ± 201 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

    %%timeit
    key = list(counter.keys())
    key.sort()
    newlist = [0] * len(key)
    for j,val in enumerate(key):
        newlist[j] = val,counter[val]

    使用列表排序后 输出  18.1 µs ± 67.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

    结论,内建函数并非最优函数,在某些情况下,需要效率就要考虑函数的使用的必要性。

    3、字符表a-z,随机挑选2个字母组成字符串,共挑选100个,降序输出所有不同的字符串及重复次数。

    alpha = 'abcdefghijklmnopqrstuvwxyz'
    words = [random.choice(alpha) + random.choice(alpha) for _ in range(100)]
    d={}
    for i in words:
        d [i] = d.get(i,0) + 1
    
    newlist = sorted(d.items(),reverse=True)
    print(newlist)

     使用formkeys

    import random
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    words = [random.choice(alpha) + random.choice(alpha) for _ in range(100)]
    d=dict.fromkeys(words,0)
    for i in words:
        d[i] += 1
    print(sorted(d.items(),reverse=True),len(d))

    使用工厂化函数

    import random
    from collections import defaultdict
    nums = [random.randint(-1000,1000) for _ in range(100)]
    counter=defaultdict(int)
    
    for i in nums:
        counter[i] = counter[i] + 1

    使用OrderedDict

    from collections import OrderedDict
    
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    l = [random.choice(alpha)+random.choice(alpha) for _ in range(100)]
    l = sorted(l,reverse=True)
    counter = OrderedDict()
    for i in l:
        counter[i] = counter.setdefault(i,0) + 1
    print(counter)
  • 相关阅读:
    用js添加网页标题时,在QQ里无效,标题栏空白
    用css3的@keyframes里设置transform:rotate(); 当控制动画暂停:animation-play-state:paused暂停,在微信和safari里无效
    Python可变序列中的一些坑,记得多注意
    你知道?Python 中的序列类型支持哪些公共操作吗?
    用 python print() 函数实现的三个特效
    教你使用python生成器重构提取数据方法,来优化你的爬虫代码
    python中主线程与子线程的结束顺序,你知道吗?
    python装饰器实现对异常代码出现进行自动监控
    Python教程:高效率遍历文件夹寻找重复文件
    python教程: filter()和reduce()函数用法详解
  • 原文地址:https://www.cnblogs.com/rprp789/p/9481112.html
Copyright © 2011-2022 走看看