Python for 循环匹配不同索引的列表项

Python for loop matching list items at different indexes

我的数据结构如下:

data=
[
(120,150,150,160,"word1"),
(152,150,170,160,"word2"),
(172,150,200,160,"word3"),
(202,290,240,300,"word4"),
(300,150,350,160,"word5"),
(202,200,240,210,"word6"),
(242,200,260,210,"word7")
]

我想 return 数据 中当前列表的第 3 个数字与下一个项目的第一个数字之间的差异小于 5 的任何单词AND 当前列表项的第 4 个数字与数组中的第 4 个数字之间的差值小于 2。然后我想将所有这些数组附加到主列表中。

所以这将是应用于数据的函数的结果:

final=
[[
(120,150,150,160,"word1"),
(152,150,170,160,"word2"),
(172,150,200,160,"word3")
],
[
(202,200,240,210,"word6"),
(242,200,260,210,"word7")
]]

word4不包括在内,因为data[2][3]-data[3][3]>2

word5不包括在内,因为data[3][2]-data[4][0]>2

我目前的尝试正确处理了 90% 的单词,但有时会组合不符合要求的单词:

temp=[]
final=[]
for i,j in enumerate(data[:-1]):
   if(j[2]-data[i+1][0]<5) and (j[3]-data[i+1][3]<2):
       if len(temp)<1:
            temp.append(j[0:4])
       temp.append(data[i+1][0:4])
   else:
       final.append(temp)
       temp=[]
if temp:
   final.append(temp)

编辑: 这是上述算法失败的真实示例:

data=
[
(38.0, 296.7943420410156, 90.86400604248047, 310.7943420410156, 'Contract'),(94.7560043334961, 296.7943420410156, 154.6480102, 310.7943420, 'Summary'), 
(250.64453125, 317.38818359375, 266.743530, 325.88818359375, 'This')
]

预期输出:

final=
[[
(38.0, 296.7943420410156, 90.86400604248047, 310.7943420410156, 'Contract'),(94.7560043334961, 296.7943420410156, 154.6480102, 310.7943420, 'Summary')
]]

实际输出:

final=
[[
(38.0, 296.7943420410156, 90.86400604248047, 310.7943420410156, 'Contract'),(94.7560043334961, 296.7943420410156, 154.6480102, 310.7943420, 'Summary'),
(250.64453125, 317.38818359375, 266.743530, 325.88818359375, 'This')
]]

您需要比较数字并添加 abs,因为差异可能为负。我还稍微美化了您的代码:

data = [
    (38.0, 296.7943420410156, 90.86400604248047, 310.7943420410156, 'Contract'),
    (94.7560043334961, 296.7943420410156, 154.6480102, 310.7943420, 'Summary'),
    (250.64453125, 317.38818359375, 266.743530, 325.88818359375, 'This')
]

temp = []
final = []
for index, item in enumerate(data[:-1]):
    if abs(item[2] - data[index + 1][0]) < 5 and abs(item[3] - data[index + 1][3]) < 2:
        if len(temp) < 1:
            temp.append(item[0:4])
        temp.append(data[index + 1][0:4])
    else:
        final.append(temp)
        temp = []
if temp:
    final.append(temp)

print(final)