首页 > 代码库 > python (2) 字典

python (2) 字典

一。字典的特性。:

  (1). python 字典是无序的。(而列表有序) 我们可以把字典理解为一组 key:value 这样的健值对。

  (2). python字典的key是唯一的。

  (3). 字典可以嵌套字典,嵌套列表。

  eg:定义一个字典。对字典进行基本操作。

技术分享
 1 >>> dict = {
 2 ... one: "1111",
 3 ... two: "2222",
 4 ... three: "3333",
 5 ... foure: "4444",
 6 ... }    #定义一个字典
 7 >>> print(dict)
 8 {two: 2222, three: 3333, foure: 4444, one: 1111}
 9 >>> dict["fave"] = "5555"    #
10 >>> dict
11 {two: 2222, three: 3333, foure: 4444, one: 1111, fave: 5555}
12 >>> dict.pop("one")    #pop函数删除
13 1111
14 >>> dict
15 {two: 2222, three: 3333, foure: 4444, fave: 5555}
16 >>> del dict["two"]    #del删除
17 >>> dict
18 {three: 3333, foure: 4444, fave: 5555}
19 >>> dict["fave"] = "5"  #
20 >>> dict
21 {three: 3333, foure: 4444, fave: 5}
22 >>> dict.get("four")    
23 >>> dict.get("foure")    #查,字典中不存在不会报错,而是返回 none
24 4444
25 >>> dict["foure"]    #查,字典中不存在会报错
26 4444
27 >>> "four" in dict
28 False
29 >>> "four" not in dict
30 True
View Code

 

python (2) 字典