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

What is the specification of python dictionary merging

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the specification for merging python dictionaries". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Dictionary merge returns a new dictionary, which is merged by the left and right operands, each of which must be dict (or instances of the subclass). If one of the two operands has a key, the last value that appears (that is, the value of the Operand from the right) is overwritten.

> d = {'spam': 1,' eggs': 2, 'cheese': 3} > e = {' cheese': 'cheddar',' aardvark': 'Ethel'} > d | e {' spam': 1, 'eggs': 2,' cheese': 'cheddar',' aardvark': 'Ethel'} > e | d # does not conform to the exchange law The left and right interchange operands will get different results {'aardvark':' Ethel', 'spam': 1,' eggs': 2, 'cheese': 3}

2. The behavior of extended assignment is exactly the same as the dictionary's update method, and supports the implementation of mapping protocols (more accurate implementation of keys and _ _ getitem_ methods) or duplicate objects.

> > d | [('spam', 999)] # in the chapter "principles", it is mentioned that the types of limited operands are not dictionaries or dictionary subcategories, or Traceback (most recent call last):... TypeError: can only merge dict (not "list") to dict > d | = [(' spam', 999)] # the chapter on "principles" mentions allowing local operators to accept a wider range of types, which behaves the same as update Accept key value pair iterative object > > d {'eggs': 2,' cheese': 'cheddar',' aardvark': 'Ethel',' spam': 999} what is the specification for python dictionary merging? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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