Как найти следующий элемент, который соответствует предыдущему элементу в пределах допуска в списке списков? - PullRequest
0 голосов
/ 07 июля 2019

У меня есть большой список отсортированных подсписков, содержащих списки, как показано ниже:

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) не выполняется, то я хочу найти следующее совпадение элемента [i ] [1] в списке, который удовлетворяет вышеуказанному условию - распечатать соответствующий элемент, а затем распечатать пропущенные элементы: </p>

например, в подсписке

[[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 голосов
/ 07 июля 2019

@ 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
0 голосов
/ 07 июля 2019

У вас почти это было, просто создайте новый цикл внутри с 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]]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...