Get the App
SLTechnology News&Howtos  ›  Development  › 

How Python programming merges according to the values of the same keys in the dictionary list

Shulou Source: shulou.com Published: 2022-06-03 07:56:59 09月14日 Update

Python programming how to merge according to the dictionary list of the same key values, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

I. Preface

Today, a fan asked a question. He now has two lists, both of whose elements are dictionaries, and both dictionaries have a key of id. Now he wants to merge the two dictionaries into one dictionary according to id.

The data for the two lists are: a_list = [{'id': 1,' value': 11}, {'id': 2,' value': 22}, {'id': 3,' value': 33}] b_list = [{'id': 1,' name':'a'}, {'id': 2,' name':'b'}, {'id': 3' The expected result of the merger [{'id': 1,' name': 'asides,' value': 11}, {'id': 2,' name': 'bounds,' value': 22}, {'id': 3,' name': 'cations,' value': 33}] 2. Implementation analysis

This is the implementation code written by fans:

For i in range (len (b_list)): for an in a_list: if blist [I] ['id'] = = a [' id']: bList [I] ['value'] = a [' value'] print (b_list)

Two for loops are used to add the a_list element dictionary id value equal to the b_list element field id value to the corresponding b_list element dictionary.

In fact, two lines of code can solve this problem:

1. We can first reassemble the a_list into the form of {id:value} by deduction.

A_values = {a ['id']: a [' value'] for an in a_list}

The value of a_values is:

{1: 11, 2: 22, 3: 33}

two。 Then reassemble the value with the b_list by deductive and dictionary deconstructing and merging:

Res_list = [{* * b, * * {'value': a_values [b [' id']]}} for b in b_list]

The list value after assembly is

The value of res_list is: [{'id': 1,' name': 'asides,' value': 11}, {'id': 2,' name': 'bounds,' value': 22}, {'id': 3,' name': 'cations,' value': 33}]

Complete sample code

A_list = [{'id': 1,' value': 11}, {'id': 2,' value': 22}, {'id': 3,' value': 33}] b_list = [{'id': 1,' name':'a'}, {'id': 2,' name':'b'}, {'id': 3 'name':' c'}] a_values = {a ['id']: a [' value'] for an in a_list} res_list = [{* * b, * * {value': a_values [b ['id']]}} for b in b_list] print (the value of' res_list is:', res_list)

Of course, a single line of code can also be done, directly merging the two deductions.

Res_list = [{* * b, * * {'value': {a [' id']: a ['value'] for an in a_list} [b [' id']]} for b in b_list]

But this is to write code for X. there is no need for it.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Dictionary two code element fan question help same programming clear necessary one line content preface field actually in fact for this that is the form. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple MariaDB Docker Microsoft