Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report