(record) Beginner Python article: five

Source: Internet
Author: User

#字典

Dict,dict Full Name dictionary, also known as map in other languages, is stored using key-value (Key-value) and has a very fast lookup speed.


Dict key (value) contains list and Tuple,key immutable, key can be tuple (tuple cannot become list, list variable)

>>> B = {"A": 1, "B": "Sad", "C":(1,["DSSD"])}>>> b{' A ': 1, ' B ': ' Sad ', ' C ': (1, [' DSSD '])}>>> Typ E (b) <class ' dict ' >

#set

Set is similar to Dict and is a set of keys, but does not store value. In set, there is no duplicate key. When creating a set, you need to provide a list as the input collection:


The set type is stored as key, so the type can be joined Tuple,list, the write type is ([]), and the output is a collection.

>>> A=set ([i]) >>> a{1, 2, 3}>>> type (a) <class ' dict ' >

#例子:

To put (1,[2,3]) into a dict or set.

>>> a= (all in All) >>> type (a) <class  ' Tuple ' >>>> b= (1,[2,3]) >>> type (b) <class  ' tuple ' >>>> c= {"W": 1, "G": 2}>>> d=set ([i]) >>> type (c) <class  ' Dict ' >>>>  type (d) <class  ' Set ' >>>> c["a"]=a>>> a (1, 2, 3) >> > c{' W ': 1,  ' G ': 2,  ' a ':  (1, 2, 3),  ' B ':  (1,&NBSP;2, &NBSP;3)}>>> c["B"]=b{' W ': 1,  ' G ': 2,  ' a ':  (1, 2, 3),  ' b ':  (1, [2, 3])}>>> d.add (a) >>> d{1, 2, 3,  (1, 2 ,  3)}>>> d.add (b) traceback  (most recent call last):   File   "<stdin>",  line 1, in <module>typeerror: unhashable type:   ' list ' 


This article is from the "Twist Blog" blog, please be sure to keep this source http://mahua.blog.51cto.com/11572858/1969209

(record) Beginner Python article: five

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.