Share a summary of python containers

Y2J
Release: 2017-05-02 15:07:35
Original
999 people have browsed it

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}
Copy after login

Add:

a[‘zxw']=91
Copy after login

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
Copy after login

Delete :

a.pop(key)
Copy after login

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 "", line 1, in 
TypeError: unhashable type: 'list'
>>> d[b]=99
>>> d
{(1, 2): 99, 'lyt': 90}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!