[英]Find numbers in Python list which are within a certain distance of each other
I have a list of numbers in Python. 我在Python中有一个数字列表。 It looks like this:
它看起来像这样:
a = [87, 84, 86, 89, 90, 2014, 1000, 1002, 997, 999]
I want to keep all the numbers which are within + or - 7 of each other and discard the rest. 我希望将所有数字保持在彼此的+或 - 7之内并丢弃其余的数字。 Is there a simple way to do this in Python?
有没有一种简单的方法在Python中执行此操作? I have read about the list filter method but am not sure how to get what I want working with it.
我已经阅读了有关列表过滤器方法的内容,但我不确定如何获得我想要使用它的内容。
I am new to Python. 我是Python的新手。
Update 更新
Output would ideally be [84, 86, 87, 89, 90] and another list [997, 999, 1000, 1002]. 理想情况下,输出为[84,86,87,89,90]和另一个列表[997,999,1000,1002]。 I want to retrieve the sequences and not the outliers.
我想检索序列而不是异常值。 Hope this makes sense.
希望这是有道理的。
This is algorithm problem, try this: 这是算法问题,试试这个:
def get_blocks(values):
mi, ma = 0, 0
result = []
temp = []
for v in sorted(values):
if not temp:
mi = ma = v
temp.append(v)
else:
if abs(v - mi) < 7 and abs(v - ma) < 7:
temp.append(v)
if v < mi:
mi = v
elif v > ma:
ma = v
else:
if len(temp) > 1:
result.append(temp)
mi = ma = v
temp = [v]
return result
a = [87, 84, 86, 89, 90, 2014, 1000, 1002, 997, 999]
print get_blocks(a)
Output: 输出:
[[84, 86, 87, 89, 90], [997, 999, 1000, 1002]]
If your problems allows transitive relations, ie x is in the group as long as it's at most 7 away from any element in the group, then this seems to me like a graph theory problem. 如果你的问题允许传递关系,即x在群组中,只要距离群组中的任何元素最多7个,那么这在我看来就像是一个图论问题。 To be more specific, you need to find all connected components .
更具体地说,您需要找到所有连接的组件 。
The problem itself is pretty easy to solve with a recursive algorithms. 使用递归算法很容易解决问题本身。 You would first create a dictionary in which every key would be one of the elements and every value would be a list of elements which are at most 7 apart from that element.
您首先要创建一个字典,其中每个键都是其中一个元素,每个值都是一个元素列表,与该元素相距最多7个。 For your example, you would have something like this:
对于你的例子,你会有这样的事情:
for element in elements:
connections[element] = []
visited[element] = False
for another in elements:
if abs(element - another) <= limit:
connections[element].append(another)
Which would give you something like this 哪个会给你这样的东西
{
84: [86, 87, 89, 90],
86: [84, 87, 89, 90],
...
997: [999, 1000, 1002]
...
2014: []
}
Now you need to write a recursive function which will take as input an element and a list, and it will keep adding elements in a list as long as it can find an element which is at most 7 apart from the current element. 现在你需要编写一个递归函数,它将把元素和列表作为输入,只要它能找到一个与当前元素最多7的元素,它将继续在列表中添加元素。
def group_elements(element, group):
if visited[element]:
return
visited[element] = True
group.append(element)
for another in connections[element]:
group_elements(another, group)
Somewhere in the code you also need to remember which elements you have already visited to make sure that you don't get into an infinite loop. 在代码中的某处,您还需要记住已经访问过的元素,以确保不会进入无限循环。
visited = {}
You need to call that function for every element in your list. 您需要为列表中的每个元素调用该函数。
groups = []
for element in elements:
if not visited[element]:
group = []
group_elements(element, group)
groups.append(group)
print group
This code should give the following output for your input: 此代码应为您的输入提供以下输出:
[[87, 84, 86, 89, 90], [2014], [1000, 1002, 997, 999]]
a = [87, 84, 86, 89, 90, 2014, 1000, 1002, 997, 999]
temp=a[0]
result=[]
temp1=[]
counter =len(a)
for i in a:
if i in range(temp-7,temp+7):
temp1.append(i)
if counter==1:
result.append(temp1)
else:
if temp1:
result.append(sorted(temp1))
temp1=[]
temp=i
counter=counter-1
print result
For any problem like this my first port of call is the Python itertools module . 对于像这样的任何问题,我的第一个调用端口是Python itertools模块 。 The pairwise function from that link that I use in the code is available in the more-itertools module .
我在代码中使用的那个链接的成对函数在more-itertools模块中可用。
from more_itertools import pairwise
results = []
chunk = []
a = [87, 84, 86, 89, 90, 2014, 1000, 1002, 997, 999]
a.sort()
for v1, v2 in pairwise(a):
if v2 - v1 <= 7:
chunk.append(v1)
elif chunk:
chunk.append(v1)
results.append(chunk)
chunk = []
print(results)
[[84, 86, 87, 89, 90], [997, 999, 1000, 1002]]
I haven't tested for edge cases, so it's buyer beware :) 我没有测试边缘情况,所以它的买家要小心:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.