Python--15 Dictionary: When the index is not useful

Source: Internet
Author: User
Tags iterable stdin



The dictionary is Python's only innuendo type hash



>>> brand = [' Li ning ', ' Anta Sports ', ' Adidas ']
>>> slogan = [' Everything is possible ', ' Keep Moving ', ' impossible are nothing ']
>>> print (' Li Ning's slogan is: ', slogan[brand.index (' Plum ')])
Li Ning's slogan is: everything is possible



 Dictionary is not a sequence type, it is a mapping type



String list tuples are sequence types



Creating and accessing index symbols-- curly braces



Create dictionary mode one: key value



>>> dict1 = {' Li ning ': ' Everything is possible ', ' Anta Sports ': ' Keep Moving ', ' Adidas ': ' Impossible Is nothing '}
>>> Dict1
{' Li ning ': ' Everything is possible ', ' Anta Sports ': ' Keep Moving ', ' Adidas ': ' Impossible is nothing '}
>>> print (' Anta Sports slogan: ', dict1[' Anta Sports ')
Anta Sports slogan is: Keep Moving



>>> dict2 = {1: ' One ', 2: ' Both ', 3: ' Three '}
>>> Dict2[3]
' Three '



  Create an empty Dictionary
  >>> dict3 = {}
>>> dict3
{}
>>> dict3 = dict ()
>>> dict3
{}



Create with Dict ()



>>> help (dict)



Class Dict (Object)
| Dict (), New Empty Dictionary
| Dict (mapping), new dictionary initialized from a mapping object ' s
| (key, value) pairs
| Dict (iterable), new dictionary initialized as if via:
| D = {}
| For K, V in iterable:
| D[k] = V
| Dict (**kwargs), new dictionary initialized with the name=value pairs
| In the keyword argument list. For Example:dict (one=1, two=2)



  Dict (mapping), new dictionary initialized from a mapping object ' s
>>> dict3 = dict (' F ', ' d '), (' I ', ""), (' s ', +/-), (' H ', 104), (' C ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:dict expect Ed at the most 1 arguments, got 5
>>> dict3 = dict ((' I ' , + +), (' s ', ' 104 '), (' H ', ' + '), (' C ', ') ')
>>> dict3
{' F ': +, ' I ': 104, ' s ': +, ' H ': $, ' C ': 67}



 (key, value) pairs



>>> dict4 = dict (code = ' Programming Changes the world ', draw = ' Every pen is a world ')
>>> Dict4
{' Code ': ' Programming changes the world ', ' draw ': ' Every pen is a world '}
>>> dict4 = dict (' Code ' = ' Programming changes the world ', draw = ' Every pen is a world ')
File "<stdin>", line 1
Syntaxerror:keyword can ' t be an expression






There is a change, no is created



>>> dict4[' code ' = ' Learning to program can change the world '
>>> dict4[' student ' = ' genius first step, but ... '
>>> Dict4
{' Code ': ' Learning to Program can change the world ', ' draw ': ' Every pen is a world ', ' student ': ' Genius first step, but ... '}



Dict () factory function (type) str (), int (), list (), tuple () ...



Fromkeys (...) re-creates a new dictionary



Dict.fromkeys (S[,v]), New dict with keys from S and values equal to V (v defaults to None)



>>> dict1 = {}



>>> Dict1.fromkeys ((+))
{1:none, 2:none, 3:none}
>>> Dict1.fromkeys ((+), ' number ')
{1: ' Number ', 2: ' Number ', 3: ' Number '}




>>> Dict1.fromkeys ((a), (' One ', ' one ', ' one ', ' three ')
{1: (' One ', ' one ', ' ' Three '), 2: (' One ', ' one ', ' three '), 3: (' One ', ' one ', ' three ')}
>>> Dict1.fromkeys ((1,3), ' number ')
{1: ' Number ', 3: ' Number '}
>>> Dict1
{}



Keys (), values (), items ()



>>> Dict1 = Dict1.fromkeys (range (32), ' likes ')
>>> Dict1
{0: ' likes ', 1: ' Likes ', 2: ' Like ', 3: ' Like ', 4: ' Like ', 5: ' Like ', 6: ' Like ', 7: ' Like ', 8: ' Like ', 9: ' Likes ', 10: ' Like ', 11: ' Like ', 12: ' Like ', 13: ' Like ', 14: ' Likes ', 15: ' Likes ', 16: ' Like ', 17: ' Like ', 18: ' Like ', 19: ' Like ', 20: ' Like ', 21: ' Like ', 22: ' Like ', 23: ' Like ', 24: ' Like ', 25: ' Like ', 26: ' Like ', 27: ' Like ', 2 8: ' Like ', 29: ' Like ', 30: ' Likes ', 31: ' Likes '}



>>> for Eachkey in Dict1.keys ():
.. print (Eachkey)
...



>>> for Eachvalue in Dict1.values ():
.. print (Eachvalue)
...



Returns a tuple



>>> for Eachitem in Dict1.items ():
.. print (Eachitem)
...






>>> Print (dict1[31])
Praise



Accessing elements that do not exist



>>> Print (dict1[32])
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror:32



>>> Dict1.get (+)
>>> Print (dict1.get ()) \
...
None
>>> Print (Dict1.get (32))
None
>>> Print (Dict1.get (32, ' no '))
No



 The Judgment key is in the dictionary without the member operator in the



>>> in dict1
True
>>> in dict1
False



 The sequence matches the value



 Clear () Empty the dictionary when you save user information there is a risk



>>> dict1.clear ()
>>> Dict1
{}
>>> dict1 = {}
>>> a = {' name ': ' JJ '}
>>> B = a
>>> b
{' name ': ' JJ '}
>>> a = {}
>>> a
{}
>>> b
{' name ': ' JJ '}



>>> a = b
>>> a
{' name ': ' JJ '}
>>> b
{' name ': ' JJ '}
>>> a.clear ()
>>> a
{}
>>> b
{}



Copy ()



Assignment just put a label on it, copy creates a new replication domain



>>> A = {1: ' One ', 2: ' Both ', 3: ' Three '}
>>> B = a.copy ()
>>> C = a
>>> C
{1: ' One ', 2: ' Both ', 3: ' Three '}
>>> a
{1: ' One ', 2: ' Both ', 3: ' Three '}
>>> b
{1: ' One ', 2: ' Both ', 3: ' Three '}
>>> ID (a)
139890364904936
>>> ID (b)
139890303134744
>>> ID (c)
139890364904936



 Pop () Popitem () Random bullets



>>> A.pop (2)
' Both '
>>> A.popitem ()
(3, ' three ')



 Setdefalt (Key[,value])



>>> a
{1: ' One '}
>>> a.setdefault (' small white ')
>>> a
{1: ' One ', ' Little white ': None}
>>> A.setdefault (5, ' five ')
' Five '
>>> a
{1: ' One ', ' Little white ': None, 5: ' Five '}



A.update (b) to modify a by the property of the key of B, or to modify it without adding



>>> B = {' Small white ': ' Dog '}
>>> a.update (b)
>>> a
{1: ' One ', ' Little white ': ' Dog ', 5: ' Five '}






>>> B = {' Small yellow ': ' da Dog '}
>>> a.update (b)
>>> a
{1: ' One ', ' Little white ': ' Dog ', 5: ' Five ', ' Little Yellow ': ' Da Dog '}






Python--15 Dictionary: When the index is not useful


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.