-
拉风的咖菲猫
要获得像你期望的第三个列表这样的输出,你必须做这样的事情:from statistics import mediannote = [6,8,10,13,14,17] Effective = [3,5,6,7,5,1] newList = []for index,value in enumerate(Effective): for j in range(value): newList.append(note[index])print(newList)print("Median is {}".format(median(newList)))输出:[6, 6, 6, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 17]Median is 10
-
繁华开满天机
note = [6,8,10,13,14,17] effective = [3,5,6,7,5,1]newlist=[]for i in range(0,len(note)): for j in range(effective[i]): newlist.append(note[i])print(newlist)
-
开心每一天1111
为了计算中位数,我建议你使用统计学。from statistics import mediannote = [6, 8, 10, 13, 14, 17]effective = [3, 5, 6, 7, 5, 1]total = [n for n, e in zip(note, effective) for _ in range(e)]result = median(total)print(result)输出10如果你看一下(在上面的代码中),你有:total[6, 6, 6, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 17]一个功能性的替代方法,使用重复:from statistics import medianfrom itertools import repeatnote = [6, 8, 10, 13, 14, 17]effective = [3, 5, 6, 7, 5, 1]total = [v for vs in map(repeat, note, effective) for v in vs]result = median(total)print(result)
-
翻翻过去那场雪
下面是在内部级别上迭代的一种方法,以按照 中指定的次数复制每个方法,并使用 statistics.median 获取中位数:EffectivenumberEffectivefrom statistics import medianout = []for i in range(len(note)): for _ in range(Effective[i]): out.append(note[i])print(median(out))# 10
-
动漫人物
要获取列表,您可以执行如下操作total = []for grade, freq in zip(note, Effective): total += freq*[grade]
-
呼唤远方
可以使用 np.repeat 获取包含新值的列表。note = [6,8,10,13,14,17] Effective = [3,5,6,7,5,1] import numpy as npnew_list = np.repeat(note,Effective)np.median(new_list),np.mean(new_list)