Detailed parsing of tuple and dict in Python

Source: Internet
Author: User
This article mainly describes the Python Ganso (Tuple) and Dictionary (Dict) of the relevant information, the text through the sample code introduced in very detailed, I believe that we have a certain reference value, the need for friends to see together below.

Objective

This article documents some of the Ganso (Tuple) and Dictionary (Dict) in Python data types, and describes some of the built-in methods. The following words do not say much, to see the detailed introduction.

Ganso Tuple

Features: The data in the meta-ancestor is not variable

Definition of an element: T = (1,)

>>> t= (1,) >>> type (T) <type ' tuple ' >

Special Ganso: "Variable" Ganso

>>> t= (1,2,3,[1,2,3]) >>> t[3][2] = ' Vimiix ' >>> T (1, 2, 3, [1, 2, ' Vimiix '])

It seems that the ancestor has changed, but the real change is that the elements in this list have changed, but the memory address of this list in T is unchanged.

Conclusion: The actual Ganso element contains a variable element, but the memory address of the Yongzu element is not changed, so the so-called Ganso immutable refers to the memory address that the element points to is the same

Dictionary Dict

Characteristics:

1. Dictionary is the only type of mapping in Python

2, the Dictionary key (key) must be immutable objects, because the dictionary is stored in the computer by the hash algorithm, the hash is characterized by the key to calculate the storage, if the key variable, will lead to data chaos.

>>> d = {1:3, ' Vimiix ':88}>>> type (d) <type ' Dict ' >
>>> D={[1,2,3]:100}traceback (most recent): File "<pyshell#15>", line 1, in <module> d={[1, 2,3]:100}typeerror:unhashable type: ' List ' (this indicates that list is a data type that cannot be hashed because list is a variable data type) >>>

As can be seen from this error, the dictionary key can only use immutable objects (Ganso is OK), but there is no requirement for dictionary values

Key-value pairs are separated by a colon ': ', each pair is delimited by commas ', ', and all of these are enclosed in curly braces ' {} '.

The key-value pairs in the dictionary are not sequential, so they cannot be accessed by the index, only the corresponding values can be obtained by keys

Expansion: If the definition of the process, the same key appears, the last storage time back to retain the last key value pair)

>>> d= {1:2,1:3}>>> D{1:3}

Create and access

The first method of creation is to create a key-value pair directly from the curly braces

The second way to create: use built-in functions dict() to create, note! dict()there can be only one parameter in parentheses, all of the key-value pairs are enclosed.

(1)

>>> D =dict ((+), (3,4), (5,6)) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module > D =dict ((+), (3,4), (5,6)) typeerror:dict expected at the most 1 arguments, got 3>>> D =dict ((+), (3,4), (5,6)) ) >>> D{1:2, 3:4, 5:6}

(2) You can also specify a keyword parameter

>>> d=dict (Vimiix = ' Vimiix ') >>> d{' Vimiix ': ' Vimiix '}

Here the lowercase ' vimiix ' can not add single quotes, plus will error!

(3) Dict built-in method. Fromkeys has two parameters

>>> D = Dict.fromkeys ((1, ' Vimiix '), (' Common ', ' value ')) >>> d{1: (' common ', ' value '), ' Vimiix ': (' Common ', ' value ')}>>>

In the actual production process, the use of dictionary generation to create, according to the existing data to generate the corresponding data, the data is meaningful.

Dictionary-generated chestnuts:

>>> L1 = [1,2,3]>>> L2 = [' A ', ' V ', ' Vimiix ']>>> d={a:b for a in L1 for B in l2}>>> D{1: ' Vimiix ', 2: ' Vimiix ', 3: ' Vimiix '}

Here is just a generative chestnut, but not the ideal answer, to learn how to generate one by one corresponding key-value pairs

Built-in methods for dictionaries:

get() :

Gets the value corresponding to the key, if none is found, returns the corresponding value

pop(key):

Popup key corresponding to the value, the default last

popitem():

Randomly returns and deletes a pair of keys and values (items) in the dictionary. Why is it randomly deleted? Because dictionaries are unordered, there is no so-called "last" or other order. When working, popitem() it is very efficient to work with items that need to be deleted individually.

update():

Update or add a key-value pair (rectify any mistakes Tsutomu)

>>> d.update ({' NewItem ': ' Update '}) >>> d{' newitem ': ' Update ', 1: ' Vimiix ', 2: ' Vimiix ', 3: ' Vimiix '}

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.