T
T
Tayrus02020-07-01 16:25:59
Python
Tayrus0, 2020-07-01 16:25:59

How to find the last common value in all the contents of a list and get the position number of the contents in the list?

There is such a list (the number of contents in it may vary):

rows = [('oops', 0, 'new1', 'AgACAgIAAxkBAAISxl78VN0h', 4, 4821, 2654700348373, '111111'),
        ('aaapp', 7000, 'test22222', 'AgACAgIAAxkBAAIVEl78g75SvLP7MlZTBkncHQ', 4, 5429, 7965418332586, '111111')]


How do I find the last common elements of a list's content and get the position number of that content? For example: there are two tuples in the rows list, these tuples have the last common elements '111111', I have to get the sequence number of the tuples in the rows list where there are common elements. That is, if I run this list through the script, I should get 0 and 1.

Example 2:
rows = [('oops', 0, 'new1', 'AgACAgIAAxkBAAISxl78VN0h', 4, 4821, 2654700348373, '111111'),
        ('aaapp', 7000, 'test22222', 'AgACAgIAAxkBAAIVEl78g75SvLP7MlZTBkncHQ', 4, 5429, 7965418332586, '111111'),
        ('oops', 0, 'new1', 'AgACAgIAAxkBAAISxl78VN0h', 4, 4821, 2654700348373, '22222'),
        ('oops', 0, 'new1', 'AgACAgIAAxkBAAISxl78VN0h', 4, 4821, 2654700348373, '22222'),]


If I run this list through the script, I should get 0 and 1 divided by 2 and 3. That is, the script should split the common elements into pairs. How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-07-01
@Tayrus0

from itertools import groupby
print(
    [
        (k, list(l))
        for k, l in
        groupby(
            enumerate(rows),
            key=lambda row: row[1][-1]
        )
    ]
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question