Python实用黑科技——找出序列里面出现次数最多的元素
2017-09-09 09:38
661 查看
需求:
如何从一个序列中快速获取出现次数最多的元素。
方法:
利用collections.Counter类可以解决这个问题,特别是他的most_common()方法更是处理此问题的最快途径。比如,现在有一个单词的序列,你想快速获取哪个单词出现频率最高,就可以这么做:
事实上,Counter对象是一个元素和其数目对应关系所构成的字典, 例如:
扩展:
如果你想手动扩展单词数目,可以使用下面的方式:
Counter类还有一些类似于数学运算的方法,使用起来也是相当方便:
如何从一个序列中快速获取出现次数最多的元素。
方法:
利用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 cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
- python 找出序列中出现次数最多的元素方法。
- python找出序列中出现次数最多的元素之Counter对象
- python 找出序列中出现次数最多的元素方法
- 找出数组里面元素出现次数最多的问题
- 找出数组里面元素出现次数最多的问题
- 利用Python找出序列中出现最多的元素示例代码
- [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
- 找出一个列表中出现次数最多的元素
- python(dict字典相关知识以及小例子:生成一个列表,存放100个随机整数,找出出现次数最多的数字)
- Python第三方库——Numpy_怎么得到向量中出现次数最多的元素值?
- 字符串String=“adadfdfseffserfefsefseetsdg”,找出里面出现次数最多的字母和出现的次数.
- 1.12.在序列中查找出现次数最多的元素
- java找出一个数组中出现次数最多的那个元素(用HashSet很新颖)
- 输出列表中出现次数最多的元素 分类: python 2013-01-15 15:25 990人阅读 评论(0) 收藏
- 找出数组中出现次数最多的前k个元素[leetcode题]
- LeetCode--Majority Element(出现次数最多的元素)Python
- java找出一个数组中出现次数最多的那个元素
- Majority Element 找出一个数组中出现次数最多的元素
- java找出一个数组中出现次数最多的那个元素