In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to count the occurrence frequency of elements in the Python sequence". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In a random sequence, find the three elements with the highest number of occurrences. How many times do they appear?
Carry on the word frequency statistics to the words of an English article, find the 10 words with the highest frequency of occurrence, how many times is it?
General practice:
From random import randint# # uses list parsing to generate 30 elements (in the range of 0,20) data = [randint (0Magne20) for _ in xrange (30)] print type (data) # uses lists to create dictionaries. Data is the key value, value is 0c = dict.fromkeys (data,0) print c # uses for loop to traverse data and encounters an x The counter c [x] increments 1for x in data: C [x] + = 1print cc1= {KGV for kdirection v in c.iteritems ()} print entries sort the dictionary entries according to the value of the dictionary, d [1] is the value. D [0] is the key stat = sorted (c.iteritems (), key= lambda dvd [1], reverse=True) print stat
Find the three elements with the highest occurrence times in a random sequence
From random import randintfrom collections import Counterdata = [randint (0pime20) for _ in xrange (30)] c2 = Counter (data) # several values are required for input smax = c2.most_common (5) smin = c2.most_common () [:-6 for Mur1] print smaxprint smin
Make statistics on the word frequency of an English article.
Import retxt = open ('code.txt'). Read () # print txt# Segmentation: through the non-alphabetical characters word = re.split ('\ Warriors)) # print wordfrom collections import Counterc3 = Counter (word) # print c3print c3.most_common (10) "how Python statistics the occurrence frequency of elements in the sequence" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.