找出一组数里出现频率最高的3个数(1.3)
Posted wangjiale1024
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找出一组数里出现频率最高的3个数(1.3)相关的知识,希望对你有一定的参考价值。
产生一个随机数组和空字典
>>> from random import randint >>> l1 = [randint(1, 20) for x in range(60)] >>> d1 = dict.fromkeys(range(1,21), 0) print(l1) [13, 3, 12, 14, 8, 10, 11, 17, 11, 10, 6, 2, 2, 7, 12, 17, 11, 6, 4, 4, 19, 13, 18, 18, 16, 9, 12, 12, 5, 6, 15, 11, 2, 4, 7, 1, 17, 6, 13, 3, 7, 7, 20, 6, 7, 15, 20, 4, 19, 11, 9, 10, 1, 14, 13, 8, 16, 17, 14, 20] >>> print(d1) {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0}
对字典排序
>>> for x in l1: d1[x] += 1 >>> d2 = sorted(d1.items(), key = lambda kv: kv[1], reverse=True) >>> return d2[0][0], d2[1][0], d2[2][0] #打印一下d2 >>> d2 [(6, 5), (7, 5), (11, 5), (4, 4), (12, 4), (13, 4), (17, 4), (2, 3), (10, 3), (14, 3), (20, 3), (1, 2), (3, 2), (8, 2), (9, 2), (15, 2), (16, 2), (18, 2), (19, 2), (5, 1)]
写两个对字典排序的方法
# 按key排序之后生成字典 >>> print({k:d1[k] for k in sorted(d1.keys(), reverse = True)}) {20: 3, 19: 2, 18: 2, 17: 4, 16: 2, 15: 2, 14: 3, 13: 4, 12: 4, 11: 5, 10: 3, 9: 2, 8: 2, 7: 5, 6: 5, 5: 1, 4: 4, 3: 2, 2: 3, 1: 2} # 按value排序之后 sorted(d1.items(), key = lambda kv: kv[1], reverse=True) sorted(d1.items(), key = lambda x : abs(x[1] -2))
上边是常规方法,介绍一下collections Counter
# counter自己完成了我们上边生成字典的过程,并且给出了一些函数 >>> c1 = Counter(l1) >>> c1 Counter({1: 2, 2: 3, 3: 2, 4: 4, 5: 1, 6: 5, 7: 5, 8: 2, 9: 2, 10: 3, 11: 5, 12: 4, 13: 4, 14: 3, 15: 2, 16: 2, 17: 4, 18: 2, 19: 2, 20: 3}) >>> c1[1] 2 >>> isinstance(c1, dict) True # 可以看到c1也是一个字典 >>> c1.most_common(3) [(11, 5), (6, 5), (7, 5)]
练习:统计一段英文里出现频率最高的单词
# 这是python官网的一段话 >>> text = ‘‘‘Welcome! Are you completely new to programming? If not then we presume you will be looking for information about why and how to get started with Python. Fortunately an experienced programmer in any programming language (whatever it may be) can pick up Python very quickly. It‘s also easy for beginners to use and learn, so jump in! Installing Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. Even some Windows computers (notably those from HP) now come with Python already installed. If you do need to install Python and aren‘t confident about the task you can find a few notes on the BeginnersGuide/Download wiki page, but installation is unremarkable on most platforms. Learning Before getting started, you may want to find out which IDEs and text editors are tailored to make Python editing easy, browse the list of introductory books, or look at code samples that you might find helpful. There is a list of tutorials suitable for experienced programmers on the BeginnersGuide/Tutorials page. There is also a list of resources in other languages which might be useful if English is not your first language. The online documentation is your first port of call for definitive information. There is a fairly brief tutorial that gives you basic information about the language and gets you started. You can follow this by looking at the library reference for a full description of Python‘s many libraries and the language reference for a complete (though somewhat dry) explanation of Python‘s syntax. If you are looking for common Python recipes and patterns, you can browse the ActiveState Python Cookbook Looking for Something Specific? If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information. The Python web site provides a Python Package Index (also known as the Cheese Shop, a reference to the Monty Python script of that name). There is also a search page for a number of sources of Python-related information. Failing that, just Google for a phrase including the word ‘‘python‘‘ and you may well get the result you need. If all else fails, ask on the python newsgroup and there‘s a good chance someone will put you on the right track. ‘‘‘ >>> import re # re.split(‘W+‘, text)是把text根据W+分割成里表,W+代表非字母 >>> Counter(re.split(‘W+‘, text)).most_common(3) [(‘Python‘, 16), (‘a‘, 16), (‘you‘, 14)]
另一种字典排序 zip 把字典转化成tuplelist再排序
# 这种是按key排了 >>> sorted(d1.items()) # 可以把v和k对调,再排 >>> sorted(zip(d1.values(), d1.keys()), reverse=True)
注意一下d1.keys()这里, 如果是python2建议使用iterkeys()和itervalues(), 这两个是迭代器, python3里keys已经是迭代器了
以上是关于找出一组数里出现频率最高的3个数(1.3)的主要内容,如果未能解决你的问题,请参考以下文章
编写程序读取一组正数,找出它们的最大数,然后计算该数的出现次数,输入是以 0结束。比如:输入 3 5 2 5 5 5 0,程序找出最大数是 5,它出现的次数是 4。