第一版最简单的代码:
def Vote_Summary(narray): list_len = len(narray[0]) result_list = [0] * list_len list_num = len(narray) for nlist in narray: for i in range(len(nlist)): if nlist[i] == 1: result_list[i] = result_list[i]+1 for i in range(len(result_list)): if result_list[i] >= (list_num/2): result_list[i] = 1 else: result_list[i] = 0 print(result_list) narray = [[0,1,0,0,1],[1,0,0,1,0],[1,0,0,1,0]] Vote_Summary(narray)
1.首先是定义定长0列表的问题
定义定长list的表达式: list = [0] *100
之前记忆里面的是, numpy.zero(10),这是numpy包里的函数,而且出来的是数组array
第二版用了切片的代码:
import numpy def Vote_Summary(nlist): list_len = len(nlist[0]) list_num = len(nlist) result_list = [0]*list_len narr = numpy.array(nlist) for col_num in range(list_l