Filtering the first two matching items in a list

I have a list of lists sorted in ascending order, like this one:

input = [[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[3,1],[6,1],[6,2]]

I want to filter this list so that the new list contains only the first (or only) element with the corresponding integers at position 0, for example:

output = [[1,1],[1,2],[2,1],[2,2],[3,1],[6,1],[6,2]]

It would be ideal if the remaining elements (those that did not meet the criteria) would remain in the input list, and the corresponding elements would be stored separately.

How should I do it?

Thank you in advance!

Edit: Elements of index 1 can be almost any integers, for example. [[1,6],[1,7],[1,8],[2,1],[2,2]]

+4
source share
2 answers

Pandas

Although this is a bit overkill, we can use pandas for this:

import pandas as pd

pd.DataFrame(d).groupby(0).head(2).values.tolist()

d . :

>>> pd.DataFrame(d).groupby(0).head(2).values.tolist()
[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [6, 1], [6, 2]]

, , . , .

Itertools groupby islice

, itertools.groupby:

from operator import itemgetter
from itertools import groupby, islice

[e for _, g in groupby(d, itemgetter(0)) for e in islice(g, 2)]

:

>>> [e for _, g in groupby(d, itemgetter(0)) for e in islice(g, 2)]
[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [6, 1], [6, 2]]

, , ( , ).

, islice : , :

[e for _, g in groupby(d, itemgetter(0)) for e in islice(g, 2, None)]

:

>>> [e for _, g in groupby(d, itemgetter(0)) for e in islice(g, 2, None)]
[[1, 3], [1, 4], [2, 3]]
+6

collections.defaultdict :

from collections import defaultdict
from pprint import pprint

input_lst = [[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[3,1],[6,1],[6,2]]

groups = defaultdict(list)
for lst in input_lst:
    key = lst[0]
    groups[key].append(lst)

pprint(groups)

:

defaultdict(<class 'list'>,
        {1: [[1, 1], [1, 2], [1, 3], [1, 4]],
         2: [[2, 1], [2, 2], [2, 3]],
         3: [[3, 1]],
         6: [[6, 1], [6, 2]]})

[:2] , :

from itertools import chain

result = sorted(chain.from_iterable(x[:2] for x in groups.values()))

print(result)

:

[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [6, 1], [6, 2]]
+2

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


All Articles