T
T
Ti-Jey2020-06-11 14:13:17
Python
Ti-Jey, 2020-06-11 14:13:17

How to check the values ​​of one dictionary by key, in another dictionary by key and display a new dictionary, with the values ​​of the first dictionary that are not in the 2nd one?

There are 2 similar dictionary lists:

s_small_sort =[{'ID': '174569', 'Name': 'Авдеев Корней', 'Age': '54'}, {'ID': '591886', 'Name': 'Авдеев Корней', 'Age': '24'}, {'ID': '392316', 'Name': 'Агафонов Остап', 'Age': '20'}, {'ID': '278400', 'Name': 'Аксёнов Натан', 'Age': '16'}, {'ID': '293922', 'Name': 'Алексеев Тимофей', 'Age': '29'}, {'ID': '604143', 'Name': 'Алексей Тeрeщенко', 'Age': '40'}, {'ID': '66721', 'Name': 'Артемьев Назарий', 'Age': '43'}, {'ID': '517124', 'Name': 'Артемьев Назарий', 'Age': '37'}]


s_big = [{'ID': '298835', 'Name': 'Лихачёв Аввакуум', 'Age': '76'}, {'ID': '720141', 'Name': 'Лихачёв Аввакуум', 'Age': '56'}, {'ID': '27164', 'Name': 'Суворов Август', 'Age': '31'}, {'ID': '469154', 'Name': 'Суворов Август', 'Age': '20'}, {'ID': '3290720', 'Name': 'Бибиков Авдей', 'Age': '19'}, {'ID': '3739928', 'Name': 'Юматов Авдей', 'Age': '14'}, {'ID': '4703064', 'Name': 'Алленов Авдей', 'Age': '24'}, {'ID': '4674783', 'Name': 'Нотович Агап', 'Age': '39'}, {'ID': '1443930', 'Name': 'Дудаков Агафон', 'Age': '24'}, {'ID': '2262161', 'Name': 'Круминь Агафон', 'Age': '82'}, {'ID': '2918131', 'Name': 'Мартюшев Агафон', 'Age': '81'}, {'ID': '4503953', 'Name': 'Мичурин Агафон', 'Age': '51'}, {'ID': '1405294', 'Name': 'Слобожанина Агафья', 'Age': '19'}, {'ID': '1430420', 'Name': 'Хмельнова Агафья', 'Age': '14'}, {'ID': '1862644', 'Name': 'Расторгуева Агафья', 'Age': '19'}, {'ID': '2835076', 'Name': 'Ямлиханова Агафья', 'Age': '15'}, {'ID': '4259886', 'Name': 'Федорова Агафья', 'Age': '22'}]


Need to find people in s_small_sort that are not in s_big by last name and display them in a separate dictionary?

for slov1 in s_small_sort:
    for slov2 in s_big:
        if (slov1['Name'].split()[0]) not in (slov2['Name'].split()[0]):
            print(slov1['Name'])


It displays each value for me on iteration, but I want only those that are not in the entire dictionary,
I understand that print () is in the second cycle, but how to display it in the first cycle?

The main question is to still find people in s_small_sort who are not in s_big by last name and create a new dictionary with all keys (ID, Name, Age)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-06-11
@Ti-Jey

new = []

for person in s_small_sort:

    found = False

    for second_person in s_big:
        if person['Name'].split()[1] == second_person['Name'].split()[1]:
            found = True
            break

    if not found:
        new.append(person)

print(new)

A
Alan Gibizov, 2020-06-11
@phaggi

s_small_sort =[{'ID': '174569', 'Name': 'Авдеев Корней', 'Age': '54'}, {'ID': '591886', 'Name': 'Авдеев Корней', 'Age': '24'}, {'ID': '392316', 'Name': 'Агафонов Остап', 'Age': '20'}, {'ID': '278400', 'Name': 'Аксёнов Натан', 'Age': '16'}, {'ID': '293922', 'Name': 'Алексеев Тимофей', 'Age': '29'}, {'ID': '604143', 'Name': 'Алексей Тeрeщенко', 'Age': '40'}, {'ID': '66721', 'Name': 'Артемьев Назарий', 'Age': '43'}, {'ID': '517124', 'Name': 'Артемьев Назарий', 'Age': '37'}]
s_big = [{'ID': '298835', 'Name': 'Лихачёв Аввакуум', 'Age': '76'}, {'ID': '720141', 'Name': 'Лихачёв Аввакуум', 'Age': '56'}, {'ID': '27164', 'Name': 'Суворов Август', 'Age': '31'}, {'ID': '469154', 'Name': 'Суворов Август', 'Age': '20'}, {'ID': '3290720', 'Name': 'Бибиков Авдей', 'Age': '19'}, {'ID': '3739928', 'Name': 'Юматов Авдей', 'Age': '14'}, {'ID': '4703064', 'Name': 'Алленов Авдей', 'Age': '24'}, {'ID': '4674783', 'Name': 'Нотович Агап', 'Age': '39'}, {'ID': '1443930', 'Name': 'Дудаков Агафон', 'Age': '24'}, {'ID': '2262161', 'Name': 'Круминь Агафон', 'Age': '82'}, {'ID': '2918131', 'Name': 'Мартюшев Агафон', 'Age': '81'}, {'ID': '4503953', 'Name': 'Мичурин Агафон', 'Age': '51'}, {'ID': '1405294', 'Name': 'Слобожанина Агафья', 'Age': '19'}, {'ID': '1430420', 'Name': 'Хмельнова Агафья', 'Age': '14'}, {'ID': '1862644', 'Name': 'Расторгуева Агафья', 'Age': '19'}, {'ID': '2835076', 'Name': 'Ямлиханова Агафья', 'Age': '15'}, {'ID': '4259886', 'Name': 'Федорова Агафья', 'Age': '22'}]
filtered_names = []
for name in (i['Name'] for i in s_big):
    if name not in (i['Name'] for i in s_small_sort):
        filtered_names.append(name)
print(filtered_names)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question