Python day two-basic use of dictionary types explained

Source: Internet
Author: User

Students who have studied Java know that there is a data format for key-value pairs in Java--map

In Python, there is a format similar to map, which is the dictionary (Dict)

1. Create a Dictionary type Object

user={    "username":"Dylan",     "  Age ":",    "password":"123" }

2. Traverse

User={    "username":"Dylan",    " Age":" -",    "Password":"123"} forKvinchUser.items ():Print(k +":"+V);

Results: Username:dylan
Age:18
Password:123

3.Clear () method--Clears all elements in the dictionary
 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }user.clear ();  print  (user); 

The result is:{}

4.Copy () copies one part into another dictionary

 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }user2  =user.copy ();  print  (user2); 

Result:{' password ': ' 123 ', ' username ': ' Dylan ', ' age ': ' + '}

5.Fromkeys(): Do not preserve the value of the source dictionary, create a new dictionary

 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }user2  =dict.fromkeys (user);  print  (user2); 

Result:{' username ': none, ' age ': none, ' Password ': none}

6.Get(): Get the value by key

User={    "username":"Dylan",    " Age":" -",    "Password":"123"}username=user.get ("username");Print(username);

Results:Dylan

7. Items (): Get all the entries for the dictionary (item)

 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }arr  =user.items ();  print  (arr); 

Results:dict_items ([' Age ', ' + '), (' username ', ' Dylan '), (' Password ', ' 123 ')])

8.keys(): Get all keys
 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }arr  =user.keys ();  print  (arr); 

Results:dict_keys ([' username ', ' password ', ' age '])

9.Pop(): Removes the item item of the specified key

User={    "username":"Dylan",    " Age":" -",    "Password":"123"}user.pop (" Age");Print(user);

Result:{' password ': ' 123 ', ' username ': ' Dylan '}

10. Popitem ()-Removes the first item in a dictionary

 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }user.popitem ();  print  (user); 

Result:{' age ': ' + ', ' password ': ' 123 '}

Items(): Get all the values of the dictionary

 user={  "  Username   ": "   Dylan   "   " age  " :  " 18  "   ,   password  " :  " 123  "  }values  =user.values ();  print  (values); 

Results:dict_values (['], ' Dylan ', ' 123 ')

del keyword-deletes the specified item according to the specified key

User={    "username":"Dylan",    " Age":" -",    "Password":"123"}deluser["username"]Print(user);

Result:{' age ': ' + ', ' password ': ' 123 '}

------------------------------------------------------End-------------------------------------------------------

I will soon fall down, but will tell me, perseverance is victory, I work until the sun to see tomorrow!

Python day two-basic use of dictionary types explained

Related Article

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.