我有一个包含数百万个数字的列表。我想知道有序列表中每个数字之间的差异对于整个列表是否相同。
list_example = [ 0, 5, 10, 15, 20, 25, 30, 35, 40, ..等等等等]
最好的方法是什么?
我的尝试:
import collections
list_example = [ 0, 5, 10, 15, 20, 25, 30, 35, 40 ]
count = collections.Counter()
for x,y in zip(list_example[0::],list_example[1::]):
print x,y,y-x
count[y-x] +=1
if len( count ) == 1:
print 'Differences all the same'
结果:
0 5 5
5 10 5
10 15 5
15 20 5
20 25 5
25 30 5
30 35 5
35 40 5
Differences all the same
原文由 Martlark 发布,翻译遵循 CC BY-SA 4.0 许可协议
这里的直接方法是最好的: