簡體   English   中英

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")
]

我想返回數據中當前列表的第 3 個數字與下一個項目的第一個數字之間的差異小於 5 並且當前列表項目的第 4 個數字與第 4 個數字之間的差異小於 5 的任何單詞2 在一個數組中。 然后我想 append 所有那些 arrays 到主列表。

所以這將是 function 應用於數據的結果:

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')
]

預計 output:

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

實際 output:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM