M
M
MaratGan2020-04-26 19:09:52
Python
MaratGan, 2020-04-26 19:09:52

How to make one from two arrays?

Hello!

There are two arrays:

First

spoiler

[{'id': 14, 'alias': 'maks', 'name': 'Макс', 'disabled': False},
{'id': 4, 'alias': 'rgs', 'name': 'Росгосcтрах', 'disabled': False},
{'id': 12, 'alias': 'vsk', 'name': 'ВСК', 'disabled': False},
{'id': 9, 'alias': 'soglasie', 'name': 'Согласие', 'disabled': False},
{'id': 3, 'alias': 'renins', 'name': 'Ренессанс', 'disabled': False},
{'id': 2, 'alias': 'ingos', 'name': 'Ингосстрах', 'disabled': False},
{'id': 7, 'alias': 'tinkoff', 'name': 'Тинькофф', 'disabled': False},
{'id': 13, 'alias': 'mafin', 'name': 'Mafin', 'disabled': False},
{'id': 8, 'alias': 'ugsk', 'name': 'Югория', 'disabled': False},
{'id': 1, 'alias': 'zetta', 'name': 'Зетта', 'disabled': False},
{'id': 0, 'alias': 'alfa', 'name': 'Альфа', 'disabled': False, 'retentionType': 1},
{'id': 17, 'alias': 'osk', 'name': 'ОСК', 'disabled': False},
{'id': 15, 'alias': 'absolute', 'name': 'Абсолют Страхование', 'disabled': False},
{'id': 16, 'alias': 'euroins', 'name': 'ЕВРОИНС', 'disabled': False},
{'id': 5, 'alias': 'sds', 'name': 'СДС', 'disabled': True},
{'id': 18, 'alias': 'guidehins', 'name': 'Гайде', 'disabled': False},
{'id': 19, 'alias': 'astrovolga', 'name': 'Астро-Волга', 'disabled': False},
{'id': 11, 'alias': 'reso', 'name': 'РЕСО', 'disabled': True, 'retentionType': 1},
{'id': 6, 'alias': 'sngi', 'name': 'Сургутнефтегаз', 'disabled': True},
{'id': 10, 'alias': 'svrezv', 'name': 'Сервис резерв', 'disabled': True}]


Second
spoiler

[{'company': 0, 'base': 4390, 'price': 3817.32},
{'company': 1, 'base': 4118, 'price': 3580.81},
{'company': 2, 'base': 4560, 'price': 3965.15},
{'company': 3, 'base': 4430, 'price': 3852.11},
{'company': 4, 'base': 4550, 'price': 3956.45},
{'company': 5, 'base': 4000, 'price': 3478.2},
{'company': 6, 'base': 4000, 'price': 3478.2},
{'company': 7, 'base': 4250, 'price': 3695.59},
{'company': 8, 'base': 4000, 'price': 3478.2},
{'company': 9, 'base': 4430, 'price': 3852.11},
{'company': 10, 'base': 4000, 'price': 3478.2},
{'company': 11, 'base': 4118, 'price': 3580.81},
{'company': 12, 'base': 4250, 'price': 3695.59},
{'company': 13, 'base': 4000, 'price': 3478.2},
{'company': 14, 'base': 4000, 'price': 3478.2},
{'company': 15, 'base': 4000, 'price': 3478.2},
{'company': 16, 'base': 4000, 'price': 3478.2},
{'company': 17, 'base': 4000, 'price': 3478.2},
{'company': 18, 'base': 4000, 'price': 3478.2},
{'company': 19, 'base': 4000, 'price': 3478.2}]


' id ' value of the first array = 'company' value of the second
array

'disabled': False, 'retentionType': 1},
{'company': 1, 'base': 4118, 'price': 3580.81, 'name': 'Ingosstrakh', 'disabled': False},
{'company ': 2, 'base': 4560, 'price': 3965.15, 'name': 'Ingosstrakh', 'disabled': False},
{'company': 3, 'base': 4430, 'price': 3852.11, 'name': 'Renaissance', 'disabled': False},
{'company': 4, 'base': 4550, 'price': 3956.45','name': 'Rosgosstrakh', 'disabled': False},
{'company': 5, 'base': 4000, 'price': 3478.2, 'name': 'SDS', 'disabled': True},
{'company': 6, 'base': 4000, 'price': 3478.2, 'name': 'Surgutneftegaz', 'disabled': True},
{'company': 7, 'base': 4250, 'price' : 3695.59, 'name': 'Tinkoff', 'disabled': False},
{'company': 8, 'base': 4000, 'price': 3478.2, 'name': 'Yugoriya', 'disabled': False },
{'company': 9, 'base': 4430, 'price': 3852.11, 'name': 'Consent', 'disabled': False},
{'company': 10, 'base': 4000, ' price': 3478.2, 'name': 'Reserve Service', 'disabled': True},
{'company': 11, 'base': 4118, 'price': 3580.81, 'name': 'RESO', 'disabled': True, 'retentionType': 1},
{'company': 12, 'base': 4250, 'price': 3695.59, 'name': 'VSK', 'disabled': False},
{'company' : 13, 'base': 4000, 'price': 3478.2, 'name': 'Mafin', 'disabled': False},
{'company': 14, 'base': 4000, 'price': 3478.2, 'name': 'Max', 'disabled': False},
{'company': 15, 'base': 4000, 'price' : 3478.2, 'name': 'Absolute Insurance', 'disabled': False},
{'company': 16, 'base': 4000, 'price': 3478.2, 'name': 'EUROINS', 'disabled': False},
{'company': 17, 'base': 4000, 'price': 3478.2, 'name': 'OSK', 'disabled': False},
{'company': 18, 'base': 4000, 'price': 3478.2, 'name': 'Gaide', 'disabled': False},
{'company': 19, 'base': 4000, 'price': 3478.2, 'name': 'Astro-Volga', ' disabled': False}]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2020-04-26
@MaratGan

companiesDict = { n['id']: { k: n[k] for k in ('name', 'disabled') } for n in arr1 }
arr3 = [ { **n, **companiesDict.get(n['company'], {}) } for n in arr2 ]

C
chupasaurus, 2020-04-26
@chupasaurus

Given the sorted second list and the uselessness of id

for item in list1:
    index = item.pop('id')
    list2[index].update(item)

D
Dimonchik, 2020-04-26
@dimonchik2013

two for loops , check and assignment
or Panda or lists comprehentions if needed faster

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question