myarray = [ [1, 0, 1],
[2, 3, 4]]
list_vk = [*zip(*myarray)] # 轉置成 [1, 2], [0, 3], [1, 4]
>>>
[(1, 2), (0, 3), (1, 4)]
#列出 x_pair[0] == 1 的那些 vk[1]
list_valid_key = list(vk[1] for vk in list_vk if vk[0] == 1)
>>>
[2, 4]
sum(list_valid_key)/len(list_valid_key)
----------------------------------------------
def get_avg(list_valid_key):
list_vk = [*zip(*list_valid_key)]
list_valid_key = list(vk[1] for vk in list_vk if vk[0] == 1)
if len(list_valid_key) > 0:
return 1, sum(list_valid_key)/len(list_valid_key)
else:
return 0, 0