【发布时间】:2019-01-30 20:35:48
【问题描述】:
给定
list =['a','b','c']
如何在循环列表 x 次时获取项目被访问的次数。例如:
# if we cycled list 4 times, output would be
output = [('a',2), ('b',1), ('c',1)]
# if we cycled list 7 times, output would be
output = [('a',3), ('b',2), ('c',2)]
是否有一个公式,或者循环是必要的?
【问题讨论】:
-
提示:假设你做了 7 个“跳”,每个项目的访问次数是多少?
-
是的,这有一个“逻辑”,不需要循环。
-
output = [('a',3), ('b',3), ('c',1)]对 7 来说是错误的...只需除以数字即可。按列表长度计算的次数,并查看商和余数,您可以使用%
标签: python algorithm math itertools discrete-mathematics