我有以下脚本,用于将一个数组中的值计数到另一个数组中。
array_1 = [1,2,0,5,7,0]
array_2 = [1,0,1,1,9,6]
# on array 2 there are 3 occurrence of 1, and 1 occurrence of zero, but because there is another zero at array_1 add 1 more. 3+2 = 5
for r in array_1:
total_count = total_count + array_2.count(r)
print("total sum: {0}".format(total_count))
当处理小数组大小时,它很好用,但是当数组大小增加时(array_1
和array_2
都为100万),就会遇到困难。有更好的方法来解决吗?
抱歉造成困扰,我稍微更新了一下问题。