【发布时间】:2013-09-20 00:14:46
【问题描述】:
我有一个未指定数字的 int 列表。我想找出列表中匹配某个值的两个整数之间的差异。
from itertools import combinations
#Example of a list
intList = [3, 6, 2, 7, 1]
diffList = [abs(a -b) for a, b in combinations(intList, 2)]
#Given if difference = 2
print diffList.count(2)
代码 sn-p 有效,但是当给出更大的列表时,我收到 MemoryError。谁能告诉我代码是否有问题或错误是由于我的硬件限制造成的?
【问题讨论】:
-
列表可以有多长?
-
@MostafaR - 可能有数百个。组合可能会达到数千..
标签: python list compiler-errors out-of-memory