Python Tour (Basic article 1-25)

Source: Internet
Author: User

Dictionary:

A dictionary is the only type of mapping in Python that stores data in the form of key-value pairs (key-value) . Python computes the hash function of key and determines the storage address of value based on the computed result, so the dictionary is stored out of order and the key must be hashed. a hash indicates that a key must be an immutable type , such as a number, a string, a tuple.

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

2.6.1, increase .

dic={'name':'Oldboy',' Age': 56,'Hobby':'Men', 1:2}dic2={7:3,'Age1': 23}dic[2]='3'                #1. Increase by KeyPrint(DIC) Dic.setdefault (2, 3)#2, increase by key value pairPrint(DIC) dic.update (DIC2)#3. Iteration IncreasePrint(DIC)
The method of increasing dict.

2.6.2, delete .

dic={'name':'Oldboy',' Age': 56,'Hobby':'Men', 1:2}dic2={7:3,'Age1': 23}#dic[2]= ' 3 ' #1. Increase by Key#print (DIC)#Dic.setdefault (2,3) #2, increased by key value pair#print (DIC)#dic.update (Dic2) #3. Iteration Increase#print (DIC)Dic.pop ('name')#1, delete by keyPrint(DIC)deldic[' Age']#2, delete by key value pairPrint(DIC) dic.clear ()Print(DIC)#3. Empty the dictionaryDic.popitem ()#4. Random deletionPrint(DIC)
the method that is removed in the dictionary, 1,2,3,4

2.6.3, change .

dic={'name':'Oldboy',' Age': 56,'Hobby':'Men', 1:2}dic2={1:3,' Age': 23}dic['name']='Yimi'     #1. Change by KeyPrint(DIC) dic.update (DIC2)#2. Change through the cover of the dictionaryPrint(DIC)
How to use the dictionary

2.6.4, check .

dic={'name':'Oldboy',' Age': 56,'Hobby':'Men', 1:2}dic2={1:3,' Age': 23}Print(dic[' Age'])#1. Search by KeyPrint(Dic.get (1))Print(Dic.get ('name1','does not exist'))#2. To find the value by key, there is a return value
how dictionaries are found.

2.6.5, other operations.

dic={'name' oldboy'age '  Hobby' man ', 1:2}values=dic.values ()  Print(values) keys=Dic.keys ()print(keys)
The method of outputting all keys and value in the dictionary

The cycle of the dictionary:

dic={'name':'Oldboy',' Age': 56,'Hobby':'Men', 1:2} forKey,valueinchDic.items ():Print(Key,value)#1, print out all key-value pairs in the dictionary forKeysinchDIC:Print(keys)#2, print out all the keys in the dictionaryDic=Dic.items ()Print(DIC)#3. Turn the dictionary into a dic.items form ()
print out key in dictionary, value pair

Python Tour (Basic article 1-25)

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.