Python dictionaries and common operations __python

Source: Internet
Author: User
Dictionaries

The dictionary can be understood as an upgraded version of the list
List

list={"Dandan", "Zhangsan", "Wangwu"}

Dictionary

dic={' name ': ' Faint ', ' sex ': ' Man ', ' professional ': ' Computer Science and Technology '}

If you need to modify the "Zhangsan", you need to find its subscript, and the dictionary is based on the name (equivalent to the ID in HTML), subscript does not have a good memory dictionary and list can access multiple data. List query elements by subscript, dictionary query elements through: the previous value, such as the ' name ' in the above code, the ' Sex ' dictionary, each element consists of two parts preceded by the ID for the value dictionary common operations

print the elements in a dictionary like a list

Print (dic[' name '))
ModifyFind Assignment by ID
Temp=raw_input ("Please enter modified value")
dic[' name ']=temp
print (' Modified value is%s '%dic[' name ')
AddIf you assign a value to an ID that does not exist in a dictionary, this element is added to the dictionary
Vqq=raw_input ("Please enter QQ number")
dic[' qq ']=vqq
print ("Add QQ number is%s"%dic[' QQ '])

Image.png RemoveDel Delete the specified element delete the entire dictionary (cannot be accessed after deletion) clear () clears the entire dictionary and can be accessed after deletion
Deletes the specified element
Del Delete the entire dictionary
Empty dictionary Len () Look up the number of elements in the dictionary

An element is an ID and a value

Check the number of elements keys
View a list of all keys that contain a dictionary values
View list items that contain all values
Display the elements in a dictionary as a list Has_key

Dic.has_key (' key ') if the key is in the dictionary, returns True if there is no return false
Has_key traversal of the elements in the dictionary (similar to the For Loop traversal list) traversing all keys in the dictionary
Image.png Traversal of all the value in the dictionary
Image.png traversal of all items in the dictionary
Image.png traversal of all key-value in the dictionary (key-value pairs)
Image.png

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.