6 回答
TA贡献1995条经验 获得超2个赞
要获得像你期望的第三个列表这样的输出,你必须做这样的事情:
from statistics import median
note = [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
TA贡献1816条经验 获得超4个赞
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)
TA贡献1836条经验 获得超13个赞
为了计算中位数,我建议你使用统计学。
from statistics import median
note = [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 median
from itertools import repeat
note = [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)
TA贡献2065条经验 获得超13个赞
下面是在内部级别上迭代的一种方法,以按照 中指定的次数复制每个方法,并使用 statistics.median
获取中位数:Effective
number
Effective
from statistics import median
out = []
for i in range(len(note)):
for _ in range(Effective[i]):
out.append(note[i])
print(median(out))
# 10
TA贡献1815条经验 获得超10个赞
要获取列表,您可以执行如下操作
total = []
for grade, freq in zip(note, Effective):
total += freq*[grade]
添加回答
举报