A
A
Alexander2020-06-16 18:28:17
Python
Alexander, 2020-06-16 18:28:17

Which procedure is best?

There is a list of dictionaries in python 2.7

#несколько тысяч объектов с десятком параметров
data = [{'a': 1, 'b': 2},{'a': 2, 'b': 2},{'a': 1, 'b': 100},{'a': 1, 'b': 22},{'a': 3, 'b': 2}]
#Искомые параметры
filtr = {'a': 1}
#Вариант 1
data = [ x for x in data if set(filtr.items()).issubset(set(x.items())) ]
#Вариант 2
data = [ x for x in data if set(x.items()).issuperset(set(filtr.items())) ]
#Вариант 3 
data = list(filter(lambda x:set(filtr.items()).issubset(set(x.items())),data))


Which of the options is best? Maybe it's better not through the list generator?
new_data = []
while len(data)>0:
    item = data.pop()
    if set(filtr.items()).issubset(set(item.items())):
        new_data.insert(0,item)


The point is to get the filtered array.
At this stage, there is no target data set, maybe someone faced a similar task.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-06-16
@bacon

Well, wait, what's the problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question