Just a quick word about the metatumen (Tuple) and the dictionary (Dict) in Python

  • 2020-05-30 20:27:57
  • OfStack


This article has documented some knowledge of the meta-ancestor (Tuple) and dictionary (Dict) of Python's data types, as well as some of the built-in methods. Without further ado, let's take a look at the details.

Tuples Tuple

Features: data in the meta ancestor is immutable

Definition of 1 element: T = (1,)

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

Special progenitors: "variable" progenitors

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

It looks like the tuple has changed, but what has changed is that the elements in the [1,2,3] list have changed, but the memory address of the list in the T tuple has not changed.

Conclusion: in fact, the element of the meta-ancestor contains variable elements, but the memory address of the element in the meta-ancestor does not change, so the so-called meta-ancestor immutable refers to the memory address that the element points to is unchanged

The dictionary Dict


1. The dictionary is the only mapping type of 1 in Python

2. The key of the dictionary (KEY) must be an immutable object -- > Since the dictionary is stored in the computer through the Hash algorithm, the features of Hash are calculated and stored by KEY. If the KEY is variable, the data will be confused.

>>> D = {1:3,'vimiix':88}
>>> type(D)
<type 'dict'>

>>> D={[1,2,3]:100}
Traceback (most recent call last):
 File "<pyshell#15>", line 1, in <module>
TypeError: unhashable type: 'list' ( This prompts list Cannot be Hash The calculated data type, because list Is a variable data type )

As you can see from this error, the keys of the dictionary can only use immutable objects (the meta-ancestor is ok), but there is no requirement for the values of the dictionary

Key-value pairs are separated by a colon ':', each pair is separated by a comma ', ', and all are enclosed in curly braces' {}'

The key value pairs in the dictionary are in no order, so they can not be accessed by the index, only the corresponding values can be obtained by the key

Extension: if the same key appears during the definition process, the last key value pair will be retained when it is finally stored.

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

Create and access

The first way to create it is to create a key-value pair directly through curly braces

The second way to create it is to take advantage of built-in functions dict() To create, notice! dict() You can have only one argument in parentheses, and you want to enclose all the key-value pairs


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

(2) you can also specify keyword parameters

>>> D=dict(vimiix = 'VIMIIX')
>>> D
{'vimiix': 'VIMIIX'}

The lowercase 'vimiix' here can't be in single quotes, it will give you an error!

(3) the built-in method of dict.fromkeys has two parameters

>>> D = dict.fromkeys((1,'vimiix'),('common','value'))
>>> D
{1: ('common', 'value'), 'vimiix': ('common', 'value')}

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

Dictionary generating 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'}

This is just a generated chestnut, but it is not an ideal answer. How to generate the corresponding key value pairs of 11

Built-in methods for dictionaries:

get() :

Gets the value corresponding to the key, and if None is not found, finds the value corresponding to the return

pop(key) :

The corresponding value of key pops up, and the default is the last one

popitem() :

Randomly returns and removes 1 pair of keys and values (items) from the dictionary. Why random deletion? Because dictionaries are out of order, there is no such thing as a "last term" or any other order. If you need to delete items 1 by 1, use popitem() The method is very efficient.

update() :

Update or add 1 key value pair.

>>> D.update({'newitem':'update'})
>>> D
{'newitem': 'update', 1: 'vimiix', 2: 'vimiix', 3: 'vimiix'}


Related articles: