2017-04-06 34 views
-4

我想從一個for循環的結果創建一個結果數組,例如說我跑我的for循環結果3,3,5,6,7,8,9,1,5,我想我的數組去多達10個ID要創建的陣列看起來像 [0,0 1,1- 2,0 3,2 4,0 5,2 6,1 7,1 8,1 9,1 10,0]如何從for循環創建python結果數組?

任何幫助表示讚賞非常初學編程的

+1

什麼_exactly_是第一結果和第二之間的相關性? –

+0

堆棧搞砸了陣列它應該是陣列與2列第一列0-10和第二列多少次該數字出現 –

+0

resuls數組應該看起來像 –

回答

0

我會建議你使用dict而不是數組。其代碼如下:

sample=[3,3,5,6,7,8,9,1,5] 
def count_occurrence(sample): 
    occurrence={} 
    for i in range(10): 
     occurrence[i]=0 
    for num in sample: 
     occurrence[num]+=1 
    return occurrence 
o=count_occurrence(sample) 
print(o) 
#if you still want the array you mentioned: 
a=sorted(list(o.items())) 
print(a)