Home  >  Article  >  Backend Development  >  Share a summary of python containers

Share a summary of python containers

Y2J
Y2JOriginal
2017-05-02 15:07:351046browse

This article mainly introduces relevant information about python container summary and arrangement. Friends in need can refer to

python container summary and arrangement

list

Variable array

tuple

Immutable array

dict

Dictionary of key-value pairs

Initialization:

a={‘lyt':90}

Add:

a[‘zxw']=91

Access:

1.a [key]

If it does not exist, an error will occur.

2.a.get(key)

Does not exist and returns None

3.a.get(key,val1)

Does not exist and returns the specified val1

#Judgment:

>>>key in a
True/False

Delete :

a.pop(key)

There is a corresponding val returned, and no error is reported

Note that the key must be an immutable variable, such as a string, an integer, or a tuple. Not an array.

>>> a
[1, 2, 3]
>>> b
(1, 2)
>>> d
{'lyt': 90}
>>> d[a]=99
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: unhashable type: &#39;list&#39;
>>> d[b]=99
>>> d
{(1, 2): 99, &#39;lyt&#39;: 90}
set

A collection without duplicate keys

To create

you need to provide a list###

The above is the detailed content of Share a summary of python containers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn