A dictionary of Python data structures

Source: Internet
Author: User

1, the definition of the Python dictionary

1. Use curly braces {}, separate each key-value pair with a comma, and connect the key to the value with a colon

2, Key: Need immutable data structure, the value can be arbitrary data object

3, the dictionary is unordered, the key in the dictionary must be unique, in the dictionary to value the way is the key to find the corresponding value

A ={}c=dict () a{}c{}b= {'a':'Hello','C':' You','b':' How'}a= [1,2,3]b= ['a','b','C']d= Dict (Zip(b)) d{1:'a',2:'b',3:'C'assign a value to the item value in the dictionary= {1: at}a[1] =3a{1:3} dictionary Access b{'a':'Hello','C':' You','b':' How'}b.get ('a')'Hello'

Add: fruits{'a':'Apple','b':'Banana','g':'Grape','o':'Orange'}fruits.setdefault ('Martin', -) -Fruits.setdefault ('a','appleaaa')'Apple'fruits{'a':'Apple','b':'Banana','g':'Grape','o':'Orange','Martin': -}fruits.update ({3:5}) fruits.update ({'a':'appleupdate'}) fruits{'a':'appleupdate','b':'Banana','g':'Grape','o':'Orange','Martin': -,3:5}2SetDefault: Parameter is key and value, if key does not exist, add key:value, if key exists, do nothing. 3Update: The parameter is key and value, regardless of whether the key exists, it becomes the form of Key:value. Delete (not used in work) fruits{'a':'appleupdate','b':'Banana','g':'Grape','o':'Orange','Martin': -,3:5}fruits.pop (3)5fruits{'a':'appleupdate','b':'Banana','g':'Grape','o':'Orange','Martin': -} change:1, Assignment2, updatea{2:9,3:5,4:9}a[0] =8a{2:9,3:5,4:9,0:8}a[2] =8a{2:8,3:5,4:9,0:8}a.update ({3:Ten}) a{2:8,3:Ten,4:9,0:8} check a{2:8,3:Ten,4:9,0:8}a.get (2)8a[0]8two different values: when the key does not exist, get will not error, the [key] way to fetch the value, will be error

the traversal of a dictionary1、 forIteminch_dict:print Itme2、 forKvincha.items (): Print K,va= {1:3,5:0,'SS':123} forIteminchA:print (item) a= {1:3,5:0,'SS':123} forKvinchA.items (): Print (K,V)


When a dictionary is treated as a sequence, it refers to a sequence of key components .

A dictionary of Python data structures

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.