實際案例某隨機序列中,找到出現次數最高的三個元素,他們的出現次數是多少?
對某英文文章的單詞進行詞頻統計,找到出現次數最高的10個單詞,出現次數是多少?
from random import randint
# #使用列表解析生成30個元素(在0~20範圍內)
data = [randint(0,20) for _ in xrange(30)]
print type(data)
# 使用列表建立字典.data為key值,value為0
c = dict.fromkeys(data,0)
print c
# 使用for迴圈遍歷data,遇到乙個x,計數器c[x]就會增加1
for x in data:
c[x] +=1
print c
c1=
print c1
#根據字典的值對於字典的項進行排序,d[1]為值。d[0]為鍵
stat = sorted(c.iteritems(),key= lambda d:d[1],reverse=true)
print stat
from random import randint
from collections import counter
data = [randint(0,20) for _ in xrange(30)]
c2 = counter(data)
#傳入需要幾個數值
smax = c2.most_common(5)
smin = c2.most_common()[:-6:-1]
print smax
print smin
import re
txt = open('code.txt').read()
# print txt
# 分割詞:通過非字母字元
word = re.split('\w*',txt)
# print word
from collections import counter
c3 = counter(word)
# print c3
print c3.most_common(10)
Python教程 如何統計序列中元素的出現頻度
實際操作中,我們該如何統計序列中元素的出現頻度,這篇python實戰教程手把手教你!實際案例 from random import randint 使用列表解析生成30個元素 在0 20範圍內 data randint 0,20 for in xrange 30 print type data 使用...
Python 統計序列中元素出現次數
import sys import random from collections import counter reload sys sys.setdefaultencoding utf 8 data list random.randint 1,20 for in range 10 從1 20隨機...
慕課網 如何統計序列中元素的出現頻度
from random import randint import requests from collections import counter from lxml import etree import re 如何統計序列中元素的出現頻度 從隨機字串中 找到次數最高的三個元素 def main...