如何根据第一个数组的最后一个元素与其连续数组的第一个元素的差对数组进行分组



我是python的新手,这就是我想要做的,假设我有一个数组

array([[1.03, 1.1 ],
[1.12, 1.25],
[2.02, 2.09],
[2.15, 3.79],
[4.73, 4.8 ],
[4.89, 5.06],
[5.28, 5.35],
[5.67, 5.91],
[6.5 , 6.57],
[6.68, 9.1 ]])

我试图做的是通过比较[1.03,1.1]的最后一个元素(即"1.1"(和[1.121.25]的第一个元素(如"1.12"(,如果差值小于0.5,则定义新数组,取[1.03、1.1]的第一元素,即"1.03"和[1.121.25]的最后元素,即为"1.12",然后该新数组应与已定义数组的其他元素一起使用。所以,基本上,我是根据连续数组的最后一个元素和第一个元素的差异对数组进行分组的,我试图用最好的方式总结我的问题。如何在python中实现这一点,我试图使用循环来实现这一目标,但失败得很惨。

这应该会给你想要的结果,基本上是合并区间:

def group_intervals(a):
ans = []
curr = None
for x in a:
# no previous interval under consideration
if curr == None:
curr = x
else:
# check if we can merge the intervals
if x[0]-curr[1] < 0.5:
curr[1] = x[1]
else:
# if we cannot merge, push the current element to ans
ans.append(curr)
curr = x
# making sure, the last interval is pushed into final answer
if curr is not None:
ans.append(curr)
return ans

希望这能帮助

import numpy as np
arr = np.array([[1.03, 1.1 ],
[1.12, 1.25],
[2.02, 2.09],
[2.15, 3.79],
[4.73, 4.8 ],
[4.89, 5.06],
[5.28, 5.35],
[5.67, 5.91],
[6.5 , 6.57],
[6.68, 9.1 ]])
def process(list_):
for index in range(len(list_)-1):
if list_[index+1][0] - list_[index][1] < 0.5: #perform comparison
element_new = [list_[index][0], list_[index+1][1]] #new list element
list_[index] = element_new #replace two old elements by the new elements
list_.remove(list_[index+1])
process(list_) #recursion
break
return list_
list1 = arr.tolist() #it is a good practice to perform remove/append action on list rather than numpy array
list2 = process(list1)
arr2 = np.array(list2)
print(arr2)

可以在没有任何循环的情况下完成:

import numpy as np
arr = np.array([[1.03, 1.1 ],
[1.12, 1.25],
[2.02, 2.09],
[2.15, 3.79],
[4.73, 4.8 ],
[4.89, 5.06],
[5.28, 5.35],
[5.67, 5.91],
[6.5 , 6.57],
[6.68, 9.1 ]])
toMerge = (arr[1:,0]-arr[:-1,1])<0.5          # flag neighbours to merged
breaks  = np.insert(toMerge==False,0,False)   # lines not merged with previous
starts  = np.insert(np.argwhere(breaks),0,0)  # indexes of start lines
ends    = np.append(starts[1:],breaks.size)-1 # indexes of end lines for merges 
result  = arr[starts]                         # keep only start lines
result[:,1] = arr[ends,1]                     # assign merged end values 

输出:

print(result)
[[1.03 1.25]
[2.02 3.79]
[4.73 5.91]
[6.5  9.1 ]]

注意:这修复了我以前回答的问题

如果您不使用numpy,可以使用一个简单的循环来产生结果:

result,prevEnd = arr[:1],arr[0][1]
for line in arr[1:]:
start,end = line
if start-prevEnd<0.5: result[-1][1] = end
else:                 result.append(line)
prevEnd = end

这在Python列表上运行良好,但在numpy数组上效率非常低

最新更新