2017-04-06 791 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)