I want to create a results array from the results of a for loop for example say i ran my for loop with results 3,3,5,6,7,8,9,1,5 and i want my array to go up to 10 id want to create an array that looks like [0, 0 1,1 2,0 3,2 4,0 5,2 6,1 7,1 8,1 9,1 10,0]
any help is appreciated very beginner programmer
答案 0 :(得分: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)