如何在列表的公差范围内找到与上一个元素匹配的下一个元素?

时间:2019-07-07 01:41:14

标签: python-3.x list indexing

我有一个很大的排序子列表,其中包含如下列表:

biglist = [  
[[25368, 22348], [22348, 21234], [21230, 17750], [17754, 15924], [15924, 14490],[14491, 12780]]   
[[22390, 21242], [10140, 4260], [4260, 2686], [2686, 438]],  
[[14044, 8726], [8762, 4144], [4144, 1420]],  
[[5817, 5097], [5590, 5530], [5304, 2729], [5097, 4430], [3450, 2489], [2729, 1676] , [2489, 1618]]  
]

对于每个子列表-理想情况下

for sublist in biglist:
    for i, element in enumerate(sublist):
         abs(element[i][1] - element [i+1][0]) < 10

例如,子列表[[25368, 22348], [22348, 21234], [21230, 17750], [17754, 15924], [15924, 14490], [14491, 12780]没有任何问题,因为:

22348 - 22348 = 0  
21234 - 21230 = 4
17750 - 17754 = 4
15924 - 15924 = 0 
14490 - 14491 = 1

因此,如果上述条件(abs(element [i] [1]-element [i + 1] [0])<10])不满足,我想做的就是找到element [i]的下一个匹配项满足上述条件的列表中的] [1]-打印出匹配的元素,然后打印出跳过的元素:

例如,在子列表中

[[5817, 5097], [5590, 5530], [5304, 2729], [5094, 4430], [3450, 2489], [2729, 1676], [2489, 1618]]

代码应显示以下内容:

[5817,5097] matches [5094, 4430] within tolerance of 10 - skipped elements: [5590, 5530], [5304, 2729]
[5304, 2729] matches [2729, 1676] within tolerance of 10 - skipped elements: [5094, 4430], [3450, 2489]
[3450, 2489] matches [2489, 1618] within tolerance of 10 - skipped elements: [2729, 1676] 

,如果根本找不到匹配项,请打印:
[5590, 5530] has no match

答案:

我似乎使用以下方法得到了想要的结果:

for sublist in biglist:
    for i, element in list(enumerate(sublist))[:-1]:
        found = False
        if abs(sublist[i][1] - sublist[i+1][0]) > 10:
            for j in range(i+1, len(sublist)):
                if abs(sublist[i][1] - sublist[j][0]) < 10:
                    print(sublist[i], "matches",  sublist[j], "within tolerance of 10 - skipped elements:", sublist[i+1:j])
                    found = True
                    break
            if not found:
                print(sublist[i], "has no matches")

但是has no matches给我错误的结果:

[22390, 21242] has no matches
[14044, 8726] has no matches
[5817, 5097] matches [5097, 4430] within tolerance of 10 - skipped elements: [[5590, 5530] [5304, 2729]]
[5590, 5530] has no matches
[5304, 2729] matches [2729, 1676] within tolerance of 10 - skipped elements: [[5097, 4430], [3450, 2489]]
[5097, 4430] has no matches
[3450, 2489] matches [2489, 1618] within tolerance of 10 - skipped elements: [[2729, 1676]]
[2729, 1676] has no matches

2 个答案:

答案 0 :(得分:0)

您几乎拥有了,只需在j内创建一个新循环,该循环从i开始,沿途检查是否匹配,并在找到第一个匹配项时结束。

biglist = [
    [[25368, 22348], [22348, 21234], [21234, 17750], [17750, 15924], [15924, 14490], [14490, 12780], [12780, 9418], [9418, 7460], [7460, 4884], [4884, 4340]], 
    [[22390, 21242], [10140, 4260], [4260, 2686], [2686, 438]], 
    [[14044, 8726], [8762, 4144], [4144, 1420]], [[5817, 5097], [5590, 5530], [5304, 2729], [5097, 4430], [3450, 2489], [2729, 1676] , [2489, 1618]]
]

for sublist in biglist:
  matched = set()
  for i, element in list(enumerate(sublist))[:-1]:
    found = False
    for j in range(i+1, len(sublist)):
      if abs(sublist[i][1] - sublist[j][0]) < 10:
        print(sublist[i], "matches",  sublist[j], "within tolerance of 10 - skipped elements", sublist[i+1:j])
        matched.add(tuple(sublist[j]))
        found = True
        break
    if not found and tuple(sublist[i]) not in matched:
      print(sublist[i], "has no matches")

结果

[25368, 22348] matches [22348, 21234] within tolerance of 10 - skipped elements []
[22348, 21234] matches [21234, 17750] within tolerance of 10 - skipped elements []
[21234, 17750] matches [17750, 15924] within tolerance of 10 - skipped elements []
[17750, 15924] matches [15924, 14490] within tolerance of 10 - skipped elements []
[15924, 14490] matches [14490, 12780] within tolerance of 10 - skipped elements []
[14490, 12780] matches [12780, 9418] within tolerance of 10 - skipped elements []
[12780, 9418] matches [9418, 7460] within tolerance of 10 - skipped elements []
[9418, 7460] matches [7460, 4884] within tolerance of 10 - skipped elements []
[7460, 4884] matches [4884, 4340] within tolerance of 10 - skipped elements []
[22390, 21242] has no matches
[10140, 4260] matches [4260, 2686] within tolerance of 10 - skipped elements []
[4260, 2686] matches [2686, 438] within tolerance of 10 - skipped elements []
[14044, 8726] has no matches
[8762, 4144] matches [4144, 1420] within tolerance of 10 - skipped elements []
[5817, 5097] matches [5097, 4430] within tolerance of 10 - skipped elements [[5590, 5530], [5304, 2729]]
[5590, 5530] has no matches
[5304, 2729] matches [2729, 1676] within tolerance of 10 - skipped elements [[5097, 4430], [3450, 2489]]
[3450, 2489] matches [2489, 1618] within tolerance of 10 - skipped elements [[2729, 1676]]

答案 1 :(得分:0)

@fizzybear感谢您的回答:)

我似乎使用以下方法得到了想要的结果:

for sublist in biglist:
    for i, element in list(enumerate(sublist))[:-1]:
        found = False
        if abs(sublist[i][1] - sublist[i+1][0]) > 10:
            for j in range(i+1, len(sublist)):
                if abs(sublist[i][1] - sublist[j][0]) < 10:
                    print(sublist[i], "matches",  sublist[j], "within tolerance of 10 - skipped elements:", sublist[i+1:j])
                    found = True
                    break
            if not found:
                print(sublist[i], "has no matches")

但是has no matches给我错误的结果:

[22390, 21242] has no matches
[14044, 8726] has no matches
[5817, 5097] matches [5097, 4430] within tolerance of 10 - skipped elements: [[5590, 5530] [5304, 2729]]
[5590, 5530] has no matches
[5304, 2729] matches [2729, 1676] within tolerance of 10 - skipped elements: [[5097, 4430], [3450, 2489]]
[5097, 4430] has no matches
[3450, 2489] matches [2489, 1618] within tolerance of 10 - skipped elements: [[2729, 1676]]
[2729, 1676] has no matches
相关问题