Python数据结构方法简介四————字典
字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个键值对之间用逗号(,)分割,整个字典包括在花括号({})中,键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。1、创建字典
dict1={"a":1,"b":2,"c":"3","d":4}
dict2={"a":,"b":2,"c":"3","d":4}
dict3={"a":,"b":2,"(5,6,7,8)":"3","d":4}
2、访问字典
字典是无序的,所以它不能依靠索引进行访问,只能通过键访问值。
dict1["a"]
1
dict1["b"]
2
如果键不存在,会报keyError.
dict1['e']
Traceback (most recent call last):
Python Shell, prompt 71, line 1
KeyError: 'e'
3、修改字典
由于字典的键是唯一的,字典只能修改键所对应的值。如果键不存在,就会在字典中添加新的键值对。
dict1
{'a': 1, 'c': '3', 'b': 2, 'd': 4}
dict1['a']=10
dict1['b']=20
dict1['e']=5
print dict1
{'a': 10, 'c': '3', 'b': 20, 'e': 5, 'd': 4}
None可以做字典的键或者值
dict1['e']=None
print dict1
{'a': 10, 'c': '3', 'b': 20, 'e': None, 'd': 4}
dict1=2
print dict1
{'a': 10, 'c': '3', 'b': 20, 'e': None, 'd': 4, None: 2}
四、字典的方法
1、clear 清空字典
D.clear() -> None. Remove all items from D
dict1={"a":1,"b":2,"c":3,"d":4}
dict1.clear()
print dict1
{}
2、copy复制字典
D.copy() -> a shallow copy of D
dict1={"a":1,"b":2,"c":3,"d":4}
test=dict1.copy()
print test
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
字典的copy方法是浅拷贝
test==dict1
True
test is dict1
False
3、fromkeys创建一个新字典,以序列S中元素做字典的键,val 为字典所有键对应的初始值
dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None.
dict1.fromkeys(['a','b'],10)
{'a': 10, 'b': 10}
4、get 获取指定键的值
D.get(k[,d]) -> D if k in D, else d. d defaults to None.
print dict1
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
dict1.get('a')
1
dict1.get('b')
2
get方法与访问字典相似,但是get又成被称为安全取值,就是当键不存在时,不会导致程序崩溃,而是返回None。
dict1.get('a')
1
dict1.get('b')
2
dict1.get('e')
5、has_key 判断键是否存在,返回布尔值
D.has_key(k) -> True if D has a key k, else False
dict1.has_key('a')
True
dict1.has_key('e')
False
6、items 以列表返回可遍历的(键, 值) 元组数组
D.items() -> list of D's (key, value) pairs, as 2-tuples
dict1.items()
[('a', 1), ('c', 3), ('b', 2), ('d', 4)]
7、keys 以列表的形式返回字典的键
D.keys() -> list of D's keys
values 以列表的形式返回字典的值
D.values() -> list of D's values
dict1.keys()
['a', 'c', 'b', 'd']
dict1.values()
8、dict1.update(dict2) 把字典dict2的键/值对更新到dict1里
D.update(**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D = E If E present and lacks .keys() method, does: for (k, v) in E: D = v In either case, this is followed by: for k in F: D = F[k
dict1.update({"v":1})
print dict1
{'a': 1, 'c': 3, 'b': 2, 'd': 4, 'v': 1}
9、setdefault 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default
D.setdefault(k[,d]) -> D.get(k,d), also set D=d if k not in D
dict1.setdefault('a')
1
dict1.setdefault('r')
print dict1
{'a': 1, 'c': 3, 'b': 2, 'd': 4, 'r': None, 'v': 1}
10、pop删除字典给定键所对应的值,返回值为被删除的值
D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised
dict1={"a":1,"b":2,"c":3,"d":4}
dict1.pop('a')
1
dict1.pop('e')
Traceback (most recent call last):
Python Shell, prompt 95, line 1
KeyError: 'e'
11、popitem()随机删除字典中的一对键和值,返回值为元组,元组的元素是被删除的键值对
D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
dict1.popitem()
('c', 3)
dict1.popitem()
('b', 2)
12、 iterkeys( ) 返回字典键的一个迭代器
iteritems( ) 返回字典键-值对的一个迭代器
itervalues( ) 返回字典值的一个迭代器
这三种方法都是迭代器,常与for循环结合使用。
页:
[1]