zoukankan      html  css  js  c++  java
  • 2-3课程 比较:统计序列中元素的出现频度

    test_1

    某随机序列 [1,2,3,4,12,123,44,2,342,123,4,66,35,.....]中,找到出现次数最高的3个元素,他们出现的次数分别是多少?

    方法1

    from random import randint
    
    data = [randint(1, 20) for _ in range(30)]
    
    c = dict.fromkeys(data, 0)
    
    for i in data:
        c[i] += 1
    
    print(c)
    
    t = list(zip(c.values(), c.keys()))
    
    # 按照key值从小到大排序
    print(sorted(t, key=lambda x: x[0]))
    
    # 按照key值从大到小排序
    print(sorted(t, key=lambda x: x[0], reverse=True))
    
    # 按照value值从小到大排序
    print(sorted(t, key=lambda x: x[1]))
    
    
    {1: 3, 16: 4, 17: 4, 18: 1, 10: 1, 13: 3, 20: 1, 4: 1, 11: 2, 5: 1, 3: 1, 12: 1, 2: 2, 6: 1, 15: 1, 8: 1, 14: 2}
    [(1, 18), (1, 10), (1, 20), (1, 4), (1, 5), (1, 3), (1, 12), (1, 6), (1, 15), (1, 8), (2, 11), (2, 2), (2, 14), (3, 1), (3, 13), (4, 16), (4, 17)]
    [(4, 16), (4, 17), (3, 1), (3, 13), (2, 11), (2, 2), (2, 14), (1, 18), (1, 10), (1, 20), (1, 4), (1, 5), (1, 3), (1, 12), (1, 6), (1, 15), (1, 8)]
    [(3, 1), (2, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 8), (1, 10), (2, 11), (1, 12), (3, 13), (2, 14), (1, 15), (4, 16), (4, 17), (1, 18), (1, 20)]
    [Finished in 0.1s]
    

    方法2

    from collections  import Counter
    
    data = [randint(1, 20) for _ in range(30)]
    
    c2 = Counter(data)
    
    print(c2)
    
    print(c2.most_common(3))
    
    Counter({7: 4, 8: 3, 19: 3, 9: 2, 3: 2, 5: 2, 4: 2, 16: 2, 13: 2, 15: 1, 17: 1, 20: 1, 2: 1, 12: 1, 10: 1, 6: 1, 11: 1})
    [(7, 4), (8, 3), (19, 3)]
    [Finished in 0.1s]
    

    test_2

    对英文文章的单词,进行词频统计,找到出现次数最高的10个单词,以及他们出现的次数是多少?

    import re
    from collections import Counter
    
    content = """Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."""
    
    words = re.split('W+', content)
    
    print(words)
    
    c = Counter(words)
    
    print(c.most_common(10))
    
    
    #result
    ['Hooray', 'It', 's', 'snowing', 'It', 's', 'time', 'to', 'make', 'a', 'snowman', 'James', 'runs', 'out', 'He', 'makes', 'a', 'big', 'pile', 'of', 'snow', 'He', 'puts', 'a', 'big', 'snowball', 'on', 'top', 'He', 'adds', 'a', 'scarf', 'and', 'a', 'hat', 'He', 'adds', 'an', 'orange', 'for', 'the', 'nose', 'He', 'adds', 'coal', 'for', 'the', 'eyes', 'and', 'buttons', 'In', 'the', 'evening', 'James', 'opens', 'the', 'door', 'What', 'does', 'he', 'see', 'The', 'snowman', 'is', 'moving', 'James', 'invites', 'him', 'in', 'The', 'snowman', 'has', 'never', 'been', 'inside', 'a', 'house', 'He', 'says', 'hello', 'to', 'the', 'cat', 'He', 'plays', 'with', 'paper', 'towels', 'A', 'moment', 'later', 'the', 'snowman', 'takes', 'James', 's', 'hand', 'and', 'goes', 'out', 'They', 'go', 'up', 'up', 'up', 'into', 'the', 'air', 'They', 'are', 'flying', 'What', 'a', 'wonderful', 'night', 'The', 'next', 'morning', 'James', 'jumps', 'out', 'of', 'bed', 'He', 'runs', 'to', 'the', 'door', 'He', 'wants', 'to', 'thank', 'the', 'snowman', 'But', 'he', 's', 'gone', '']
    [('He', 9), ('the', 9), ('a', 7), ('snowman', 5), ('James', 5), ('s', 4), ('to', 4), ('out', 3), ('adds', 3), ('and', 3)]
    [Finished in 0.1s]
    
  • 相关阅读:
    P1509 找啊找啊找GF
    P1508 Likecloud-吃、吃、吃
    P1493 分梨子
    P1507 NASA的食物计划
    Java简单从文件读取和输出
    服务器和普通用户电脑的区别
    readUTF()和writeUTF()
    js中substring和substr的用法
    AfxMessageBox和MessageBox差别
    POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
  • 原文地址:https://www.cnblogs.com/botoo/p/13176126.html
Copyright © 2011-2022 走看看