执行代码countsum = [163, 211, 121, 46, 11]
sumrate= countsum
for i in sumrate:
i /= total
print(i)
print("sum rate is "+str(sumrate))得到结果0.295289855072463750.38224637681159420.219202898550724640.083333333333333330.019927536231884056sum rate is [163, 211, 121, 46, 11]python初学,不太懂这是什么原理,怎么改,谢谢
1 回答
米脂
TA贡献1836条经验 获得超3个赞
这是显而易见的,迭代器变量i并不直接引用到数组中,修改i不会修改数组的元素。
你需要这么写
# encoding: utf-8 total = 552.0 countsum = [163, 211, 121, 46, 11] sumrate = list(map(lambda i : i / total, countsum)) print("sum rate is " + str(sumrate))
结果:
sum rate is [0.29528985507246375, 0.3822463768115942, 0.21920289855072464, 0.08333333333333333, 0.019927536231884056]
添加回答
举报
0/150
提交
取消