2017-08-07 54 views
0

我寫一段代碼,通過每個記錄迭代並打印統計稱爲間隔。計數器和循環

for record in records: 

    from collections import Counter 
    count = Counter(intervals) 

    for interval, frequency in count.iteritems(): 
     print interval 
     print frequency 

輸出看起來像這樣:

Record 1 
199 
7 
200 
30 

Record 2 
199 
6 
200 
30 

在這個例子中,在記錄1中,有間隔長度199的7個實例和間隔長度200的30個實例在記錄2,有間隔長度199的6個實例和間隔長度的31個實例200 我想看到的是這樣兩個記錄總體的統計彙總,但無法弄清楚如何得到這些結果:

All Records 

199 
13 

200 
61 

在兩個記錄中,總共有13個實例的總計間隔長度爲199(7 + 6),總共有61個實例的總計爲200(30 + 31)。如上所示,我無法獲得我的記錄的總體統計摘要。

+2

能否請您編輯您的文章澄清你的問題? –

+1

對不起,但你的問題到底是什麼?另外,請修復您的縮進。 –

+1

你想知道什麼?清楚地說明... –

回答

1

您需要的變量外for loop存儲頻率計數 下面的例子可以幫助你。

from collections import Counter 


records = [[199,200,200], [200,199,200]] 
freq_dist = Counter()      # Variable store frequency distribution 

for record in records: 
    counts = Counter(record) 
    freq_dist.update(counts) 

for freq, value in freq_dist.items(): 
    print(freq, value) 

輸出:

200 4 
199 2 

參考collections.Counter

+0

循環? –

+0

好的。我知道了。我不知道'Counter'支持'update'。 – Kaushal

+0

@ juanpa.arrivillaga感謝您的建議。更新了答案。 – Kaushal