我最近一直在使用Counter().most_common
,但问题是,我需要将它显示多少的位转换成一个百分比,例如:
[(2, 5), (10, 5)]
致:
[(2, 50%), (10, 50%)]
有没有办法用Counter().most_common
或其他方法来实现这一点?
以下是我的部分代码:
while count < int(DR):
count = count + int(1)
DV.append(random.randint(1, int(DI)))
if count == int(DR):
print ('\n(The Number that was rolled , the amount of times it came up):')
global x
print (Counter(DV).most_common(int((DI))))
输出,格式为
(element, % of total)
要按顺序列出它们,可以使用
collections.Counter.most_common
如果您没有原始数据,您仍然可以使用
Counter
来实现这一点。其中:
i
是已计数的项count
是该项的计数c
是Counter
对象3
是百分比的精度如果将
sum(c.values())
移到列表压缩之外,则可以提高性能。相关问题 更多 >
编程相关推荐