需求:
如何从一个序列中快速获取出现次数最多的元素。

方法:
利用collections.Counter类可以解决这个问题,特别是他的most_common()方法更是处理此问题的最快途径。比如,现在有一个单词的序列,你想快速获取哪个单词出现频率最高,就可以这么做:

In [22]: words = ['look', 'into', 'my', 'eyes', 'look', 'into',
...: 'my', 'eyes', 'the', 'eye', 'the', 'eyes', 'not',
...: 'around', 'the', 'eyes', "don't", 'look', 'around',
...: 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're",
...: 'under'
...: ]

In [23]: from collections import Counter
In [24]: word_counts = Counter(words)
In [25]: print(word_counts.most_common(3))
[('eyes', 6), ('look', 4), ('the', 4)]

事实上,Counter对象是一个元素和其数目对应关系所构成的字典, 例如:

In [26]: word_counts['not']
Out[26]: 1
In [27]: word_counts['into']
Out[27]: 3

扩展:
如果你想手动扩展单词数目,可以使用下面的方式:

In [28]: more_words = ['why', 'are', 'you', 'not', 'looking', 'in',
...: 'my', 'eyes']
In [29]: for word in more_words:
...: word_counts[word] += 1
...: # word_counts.update(more_words)
In [30]: word_counts['eyes']
Out[30]: 7

Counter类还有一些类似于数学运算的方法,使用起来也是相当方便:

In [31]: a = Counter(words)

In [32]: b = Counter(more_words)

In [33]: a
Out[33]:
Counter({'around': 2,
"don't": 1,
'eye': 1,
'eyes': 6,
'into': 3,
'look': 4,
'my': 3,
'not': 1,
'the': 4,
'under': 1,
"you're": 1})

In [34]: b
Out[34]:
Counter({'are': 1,
'eyes': 1,
'in': 1,
'looking': 1,
'my': 1,
'not': 1,
'why': 1,
'you': 1})

In [35]: c = a + b

In [36]: c
Out[36]:
Counter({'are': 1,
'around': 2,
"don't": 1,
'eye': 1,
'eyes': 7,
'in': 1,
'into': 3,
'look': 4,
'looking': 1,
'my': 4,
'not': 2,
'the': 4,
'under': 1,
'why': 1,
'you': 1,
"you're": 1})

In [37]: d = b - a

In [38]: d
Out[38]: Counter({'are': 1, 'in': 1, 'looking': 1, 'why': 1, 'you': 1})

Python实用黑科技——找出序列里面出现次数最多的元素的更多相关文章

  1. 【python cookbook】【数据结构与算法】12.找出序列中出现次数最多的元素

    问题:找出一个元素序列中出现次数最多的元素是什么 解决方案:collections模块中的Counter类正是为此类问题所设计的.它的一个非常方便的most_common()方法直接告诉你答案. # ...

  2. 【python cookbook】找出序列中出现次数最多的元素

    问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...

  3. Python实用黑科技——找出最大/最小的n个元素

    需求: 快速的获取一个列表中最大/最小的n个元素. 方法: 最简便的方法是使用heapq模组的两个方法nlargest()和nsmallest(),例如: In [1]: import heapqIn ...

  4. python之Counter类:计算序列中出现次数最多的元素

    Counter类:计算序列中出现次数最多的元素 from collections import Counter c = Counter('abcdefaddffccef') print('完整的Cou ...

  5. Java实现找出数组中重复次数最多的元素以及个数

    /**数组中元素重复最多的数 * @param array * @author shaobn * @param array */ public static void getMethod_4(int[ ...

  6. [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法

    问题 怎样找出一个序列中出现次数最多的元素呢? 解决方案 collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案 c ...

  7. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  8. Python中用max()筛选出列表中出现次数最多的元素

    1 List = [1,2,3,4,2,3,2] # 随意创建一个只有数字的列表 2 maxTimes = max(List,key=List.count) # maxTimes指列表中出现次数最多的 ...

  9. python 找出字符串中出现次数最多的字母

    # 请大家找出s=”aabbccddxxxxffff”中 出现次数最多的字母 # 第一种方法,字典方式: s="aabbccddxxxxffff" count ={} for i ...

随机推荐

  1. linux基础命令<二>

    1.关机 init 0   poweroff   halt  shutdown –h   now 2.重启 init 6   reboot  shutdown –r now 3.查询都有那些用户在系统 ...

  2. springboot2.X版本得@Transactional注解事务不回滚不起作用

    参考文章  https://my.oschina.net/happyBKs/blog/1624482   https://blog.csdn.net/u011410529/article/detail ...

  3. Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ClamReason/article/details/23971805 首先重点推荐介绍otl介绍及使 ...

  4. ARC 100 C - Linear Approximation题解---三分法

    题目链接: https://arc100.contest.atcoder.jp/tasks/arc100_a 分析: 比赛时做这题想到一个瞎搞的方法就是在平均数上下波动一下取最小值,然后大佬yjw学长 ...

  5. 虚拟机无法启动,提示:无法打开内核功能扩展“com.vmware.kext.vmci”: 无此文件或目录

    打开 系统偏好设置->安全性与隐私->允许打开 即可

  6. SQLAlchemy技术手册

    一.ORM 框架简介 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应用开发环境中的主流开发方法 ...

  7. thinkphp3.2.3 自动验证 unique 出错的解决办法

    场景:修改数据时,唯一验证name字段出错,提示已存在. 排查: 1.传入的参数是否包含主键,因为 D('模型名')->create() 会自动判断是否是新增或者修改,根据传入的参数判断是否包含 ...

  8. Windows访问VirtualBox的Redis服务器

    一般来讲,我们不愿意在Windows上面安装太多的软件,这样会导致Windows运行太慢. 所以我在windows上面安装了VirtualBox,然后把相关的软件都安装在virtualBox里面,比如 ...

  9. centos 7 Network 脚本

    #!/bin/sh #主动启动网卡 interface=$() ifup $interface #获取当前网络信息 default_route=$(ip route show) default_int ...

  10. The Linux Kernel 4.15.0官方文档内核语言风格解读(留)

    https://www.kernel.org/doc/html/v4.15/translations/zh_CN/coding-style.html 1.缩进 制表符是 8 个字符,所以缩进也是 8 ...