from numpy import *
import collections
array = [28000,35200,35200,35200,35700,36000]
rng = range(35000,37000)
for elem in array:
print elem
35200 35700 36000
这让我有了一个良好的开端,但我只需要范围列表中的重复项,即 35200。例如,我尝试在 print elem 下添加一行 -
print elem
print [x for x, y in collections.Counter(a).items if y > 1]
但是我得到TypeError: 'numpy.float64' object is not iterable
。稍后方程式将需要该副本。如何将数组缩小到某个范围内的重复项?
最佳答案
items
是一个函数,您忘记了 ()
:
counterItems = collections.Counter(a).items()
print [x for x, y in counterItems if y > 1]
关于Python 2.7 尝试使用范围和重复来缩小列表范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083068/