Comparison of subscriptions and their merger

I have a list containing many subscriptions, which are initially pairs of numbers, so it looks like this:

list = [[2, 3], [4, 5], [7, 8], [8, 9], [11, 12], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [20, 21]]

and I want to compare the last digit of the sublist with the first digit in the next sublist, and if they match, combine them into one sublist. Thus, the output for the two corresponding subscriptions will be something like this:

output = [[7, 8, 9]]

And, of course, if there are a number of suitable sub-lists, then merge them all into one large sub-list.

output = [[14, 15, 16, 17, 18, 19]]

I was thinking about using itemgetter as a kind of key for comparison. Perhaps something like:

prev_digit = itemgetter(-1)
next_digit = itemgetter(0)

but then I realized that I really do not understand how I can use it in Python due to lack of knowledge. I tried to imagine a for loop, but that didn't work, because I did not know how to implement these “keys”.

- Python, , .

, ( , ), .

, Python, . , google, , , , .

+4
2

, . .

alist = [[2, 3], [4, 5], [7, 8], [8, 9], [11, 12], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [20, 21]]

l = [alist[0][:]]
for e in alist[1:]:
   if l[-1][-1] == e[0]:
      l[-1].append(e[1])
   else:
      l.append(e[:])

. . , . , .

l:

[[2, 3], [4, 5], [7, 8, 9], [11, 12], [14, 15, 16, 17, 18, 19], [20, 21]]

, :

>>> l = [[2, 3], [4, 5], [7, 8, 9], [11, 12], [14, 15, 16, 17, 18, 19], [20, 21]]
>>> max(l, key=len)
[14, 15, 16, 17, 18, 19]

:

>>> alist = [[2, 3], [4, 5], [7, 8], [8, 9], [11, 12], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [20, 21]]
>>> 
>>> l = [alist[0][:]]
>>> for e in alist[1:]:
...    if l[-1][-1] == e[0]:
...       l[-1].append(e[1])
...    else:
...       l.append(e[:])
... 
>>> l
[[2, 3], [4, 5], [7, 8, 9], [11, 12], [14, 15, 16, 17, 18, 19], [20, 21]]
>>> alist
[[2, 3], [4, 5], [7, 8], [8, 9], [11, 12], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [20, 21]]

. 6.4 :

$ python -mtimeit "list = [[2, 3], [4, 5], [7, 8], [8, 9], [11, 12], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [20, 21]]" "reduce(lambda x,y: x[:-1] + [x[-1] + y[1:]] if x[-1][-1] == y[0] else x + [y], list[1:], [list[0]])"
100000 loops, best of 3: 6.4 usec per loop

for 3,62 :

$ python -mtimeit "alist = [[2, 3], [4, 5], [7, 8], [8, 9], [11, 12], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [20, 21]]" "l = [alist[0][:]]" "for e in alist[1:]:" "   if l[-1][-1] == e[0]:" "      l[-1].append(e[1])" "   else:" "      l.append(e[:])"
100000 loops, best of 3: 3.62 usec per loop

Python 2.7.3. for 56% . , , , . .

+2

reduce

>>> reduce(
... lambda x,y: x[:-1] + [x[-1] + y[1:]] if x[-1][-1] == y[0] else x + [y],
... list[1:],
... [list[0]]
... )
[[2, 3], [4, 5], [7, 8, 9], [11, 12], [14, 15, 16, 17, 18, 19], [20, 21]]

lamdba , .

def mergeOverlappingRange(x, y):
    if x[-1][-1] == y[0]: 
        return x[:-1] + [x[-1] + y[1:]]
    else:
        return x + [y]

reduce(mergeOverlappingRange, list[1:], [list[0]])
+1

Source: https://habr.com/ru/post/1660623/


All Articles