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

Python -- dictionary

編輯:Python

1. Format

Variable name = {“key”:“value1”,“key2”:value2,“key3”:value3}

2.

(1). Modifying elements : You can modify elements , adopt key find , You can modify

(2). Additive elements : Variable name [“key”]=“value”

If you are using Variable name [‘ key ’] = data when , This “ key ” In the dictionary , Then modify the data , non-existent , So it's going to add this element

(3). Add new elements Variable name [‘ new key’]=“value”

(4). Remove elements del clear( Empty )

(5).len() In the measurement dictionary , The number of key-value pairs

print(len( Variable name ))

(6).keys Return a dictionary containing all KEY A list of

print( Variable name .keys())

(7).values Return a dictionary containing all value A list of

print( Variable name .value())

(8).items Return one containing all ( key , value ) The list of Yuanzu

print( Variable name .items())

3. Traversal of dictionaries

Traversing the dictionary key key

a = {“name”:“aa”,“age”:15,“sex”:“ male ”}

for key in a.keys():

print(key)

Traversing the dictionary value value

a = {“name”:“aa”,“age”:15,“sex”:“ male ”}

for value in a.values():

print(value )


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