Python data Type--dictionary

Source: Internet
Author: User

Introduced
    • We might do this without a dictionary:
>>>t = [' name ',' age ',' Sex ']>>>v = [' Chen ', -,' Male ']>>>Zip (t,v) [(' name ',' Chen '), (' age ', -), (' Sex ',' Male ')]>>>v = [' Chen ', -]>>>Zip (t,v) [(' name ',' Chen '), (' age ', -)]
    • It is also possible to use lists to store data, but this is not intuitive enough because it is accessed with subscripts. Thus, the data structure of the dictionary {} is generated.

      Dictionary is the only type of mapping (hash table) of Python
      The object of the dictionary is mutable, but the keys of the dictionary must use immutable objects, and different types of key values can be used in a dictionary.
      Keys () or values () returns a list of key or value.
      Items () returns a tuple containing key-value pairs

Create a dictionary method one: {}
>>>DIC = {0:0,1:1,2:2}>>>dic[0]0>>>dic[2]2>>>DIC = {0:' xx ',1:' One ',2:' A '}>>>dic[0]' xx '>>>DIC = {0:' 123 ', Name:' Chenwen ',' x ':456}traceback (most recent): File"<stdin>", line1,inch<module>nameerror:name' name '  is  notDefined>>>Name =None>>>DIC = {0:' 123 ', Name:' Chenwen ',' x ':456}>>>Name>>>dic{0:' 123 ',' x ':456,None:' Chenwen '}
    • Note
      In the above code, the error message, meaning, name is not defined, so after the name is defined, the dictionary can be successfully created. After creation, the key is actually the value of name.
      The value of name is not defined directly when the dictionary is created.
>>> 1>>> 2>>> dic = {a:‘aaa‘,‘b‘:‘bbb‘}>>> a1>>> b2>>> dic{1‘aaa‘‘b‘‘bbb‘}
Method Two: Using the Factory Method Dict ()
>>> Dict (one=1, two=2){' both ':2,' One ':1}>>> Dict ({' One ':1,' both ':2}){' both ':2,' One ':1}>>> dict (Zip (' One ',' both '), (1,2))){' both ':2,' One ':1}>>> Dict ([ [' Both ', 2], [' One ', 1]]){' both ':2,' One ':1}
Method Three: Built-in method: Fromkeys ()
    • The element in the dictionary has the same value and defaults to None
>>> {}.fromkeys((‘x‘,‘y‘)){‘y‘None‘x‘None}>>> {}.fromkeys((‘x‘,‘y‘),-1){‘y‘: -1‘x‘: -1}
Accessing values in a dictionary
>>> dic = {0:‘123‘,name:‘chenwen‘,‘x‘:456forin dic:...     print k...0forin dic:...     print dic[k]...123456chenwen
Update and delete
>>>DIC = {0:' 123 ', Name:' Chenwen ',' x ':456}>>>dic[' like '] ="DOTA">>>dic{0:' 123 ',' x ':456,' like ':' DOTA ',None:' Chenwen '}>>>dic[' like '] ="GIRL">>>dic{0:' 123 ',' x ':456,' like ':' GIRL ',None:' Chenwen '}>>>del(dic[None])>>>dic{0:' 123 ',' x ':456,' like ':' GIRL '}>>>Dic.pop (0)' 123 '>>>dic{' x ':456,' like ':' GIRL '}>>>Dic.clear ()>>>dic{}>>>DIC = {0:' 123 ', Name:' Chenwen ',' x ':456}>>>del(DIC)>>>Dictraceback (most recent): File"<stdin>", line1,inch<module>nameerror:name' dic '  is  notDefined>>>#注意: Clear and Del are different, clear is just emptying, but del is... #将整个变量删除, equivalent to no definition

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python data Type--dictionary

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.