Найти отсутствующие словари в списке - PullRequest
0 голосов
/ 06 ноября 2019

Мне нужно сравнить два списка словарей и извлечь элементы отсутствия, «соединение» - это атрибут, который идентифицирует объект. Другие свойства могут изменяться во времени. Я попытался с фильтрами и картой, но я все еще застрял.

ввод должен выглядеть следующим образом:

list1 = [{'username': 'user1', 'connection': "0x2083588'", 'remote': '10.0.5.251:44840','uri': 'www.google.com:443','seconds': '600'}
        ,{'username': 'user2', 'connection': "0x2a90d778'", 'remote': '10.0.5.251:44796','uri': 'mobilenetworkscoring-pa.googleapis.com:443','seconds': '600'}]


list2 = [{'username': 'user1', 'connection': "0x2083588'", 'remote': '10.0.5.251:44840','uri': 'www.google.com:443','seconds': '400'}
        ,{'username': 'user2', 'connection': "0x2a90d778'", 'remote': '10.0.5.251:44796','uri': 'mobilenetworkscoring-pa.googleapis.com:443','seconds': '400'}
        ,{'username': 'user3', 'connection': "0x2a90d678'", 'remote': '10.0.5.251:44796','uri': 'mobilenetworkscoring-pa.googleapis.com:443','seconds': '400'}]

вывод должен быть таким:

[{'username': 'user3', 'connection': "0x2a90d678'", 'remote': '10.0.5.251:44796','uri': 'mobilenetworkscoring-pa.googleapis.com:443','seconds': '400'}]

1 Ответ

0 голосов
/ 07 ноября 2019
# Mapping the list1 by "connection" property
mapList = map(lambda y: y.get('connection'), list1)

# Filtering the elements of the second list (list2) 
# that are missing from the map of the list1 (mapList) by "connection" property
filterList = filter(lambda x: x.get('connection') not in mapList,list2)
print filterList
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...