Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the specification of python dictionary merging

Shulou Source: shulou.com Published: 2022-05-31 17:49:47 10月02日 Update

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!

Tags: Dictionary Operand content principle subclass object method more knowledge chapter type behavior different practical same successful learning next two right dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei Linux vpn OPPO Reno