程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Common operation methods of dictionary dict in Python

編輯:Python

Below python Code display python Common operations of Chinese dictionary , The dictionary is python It plays an important role in development , It is very important to master dictionary operation

# Create an empty dictionary
x = {}
Create a dictionary with three items
x = {"one":1, "two":2, "three":3}
Access one of these elements
x['two']
Returns a list of all keys in the dictionary
x.keys()
Returns a list of all values in the dictionary
x.values()
Add a new project
x["four"]=4
Modify a dictionary item
x["one"] = "uno"
Delete a dictionary entry
del x["four"]
Copy a dictionary to the new variable
y = x.copy()
Clear all dictionary entries
x.clear()
Return dictionary length , Number of projects
z = len(x)
Checks whether the dictionary contains the specified key
z = x.has_key("one")
Traverse the dictionary key
for item in x.keys(): print item
Traverse the list of values in the dictionary
for item in x.values(): print item
Use if Statement to obtain the corresponding key value in the dictionary
if "one" in x:
print x['one']
if "two" not in x:
print "Two not found"
if "three" in x:
del x['three']</pre> 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved