Python:リストまたはarrayで要素の位置を特定し、要素の個数を計算する
692 ワード
import numpy as np
a = np.array([1,2,2,3,3,4,4,5,5,5,5])
b = [1,2,2,3,3,4,4,5,5,5,5]
print(type(a))
print(type(b))
# list
set_a = set(a)
set_b = set(b)
print("---list ---")
for ele in set_b:
print(" {} :{}".format(ele,b.count(ele)))
print("---list ---")
for ele in set_b:
print(" {} :{}".format(ele,b.index(ele)))
print("---nparray ---")
for ele in set_a:
print(" {} :{}".format(ele,np.sum(a == ele)))
print("---nparray ---")
for ele in set_a:
print(" {} :{}".format(ele,np.where(a == ele)))
明らかにnp.arrayはずっと強い