Filtering tuple lists with tuple elements

I work in Python (2.7.9) and am trying to filter a list of tuples with a list of elements of these tuples. In particular, my objects are as follows:

tuples = [('a', ['a1', 'a2']), ('b',['b1', 'b2']), ('c',['c1', 'c2'])]
filter = ['a', 'c']

I am new to Python, and the easiest way to filter tuples that I could find was to understand the list as follows:

tuples_filtered = [(x,y) for (x,y) in tuples if x in filter]

The resulting filtered list is as follows:

tuples_filtered = [('a', ['a1', 'a2']), ('c',['c1', 'c2'])]

Unfortunately, this list comprehension seems very inefficient. I suspect this is because my list of tuples is much larger than my filter, a list of strings. In particular, the filter list contains 30,000 words, and the list of tuples contains about 134,000 2 tuples.

The first elements of 2-tuples are significantly different from each other, but there are several instances of the repeating first elements (not sure how many, in fact, but there are not many of them in comparison with the power of the list).

My question is: Is there a more efficient way to filter the list of tuples with the list of elements of these tuples?

(Apologizes if this is off topic or cheating.)

A related question (which does not mention effectiveness):

Filter the list of tuple lists

+4
source share
1 answer

In the comment you write:

The list of filters contains 30,000 words, and the list of tuples contains about 134,000 2 tuples.

in O (N) , , 134k . , , . , , 30k, 30k * 134k == 4 .

:

filter_set = set(filter)

- O (1) ; 134k .

, , - ; , :

tuples_filtered = [tup for tup in tuples if tup[0] in filter_set]
+8

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


All Articles