Python 字典,,字典(dict):
Python 字典,,字典(dict):
字典(dict):
字典是一种通过名字或者关键字引用的数据结构,是另一种可变容器模型,且可存储任意类型对象,其键值可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一内建的映射类型。
字典的每个键值key=>value对用冒号(:)分割,每个键值对之间用逗号(,)分割,整个字典包括在花括号{}中,格式如下所示:
d = {key1 : value1, key2 : value2, key3 : value3}
键(key)必须唯一,且不可变,如字符串、数字或元组;值(value)可以重复,且可以取任何数据类型。
d = {1 : "one",2 : "two",3 : "three",4 : "one",5 : "two"}d = {"Lizi" : 18,"sd" : "dfde"}
字典是无序的:
d={3 : "three",2 : "two",4 : "four",1 : "one"}
访问字典的值:(查字典:一定要确定有该键)
d = {1 : "one",2 : "two",3 : "three",4 : "one",5 : "two"}print(d[2])#输出结果:two
增加字典元素:
d = {1 : "one",2 : "two",3 : "three",4 : "one"}d[5]="a"print(d)#输出结果:{1: ‘one‘, 2: ‘two‘, 3: ‘three‘, 4: ‘one‘, 5: ‘a‘}
删除字典元素:pop()、popitem()、del()
‘‘‘标准删除‘‘‘d = {1 : "one",2 : "two",3 : "three",4 : "one"}a=d.pop(2)print(d)print(a)#输出结果:{1: ‘one‘, 3: ‘three‘, 4: ‘one‘}#输出结果:two
‘‘‘popitem():随机删除‘‘‘d = {1 : "one",2 : "two",3 : "three",4 : "one"}d.popitem() print(d)#输出结果:{1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
‘‘‘全局删除‘‘‘d = {1 : "one",2 : "two",3 : "three",4 : "one"}del dprint(d)#输出结果:NameError: name ‘d‘ is not defined
访问字典成员:get()、in
d = {1 : "one",2 : "two",3 : "three",4 : "one"}print(d.get(2))print(4 in d) #判断d中是否存在该键值#输出结果:two#输出结果:True
打印所有的值:values()将字典中的值以列表形式返回
d = {1 : "one",2 : "two",3 : "three",4 : "one"}print(d.values())#输出结果:dict_values([‘one‘, ‘two‘, ‘three‘, ‘one‘])
打印所有的键:keys()将字典中的键以列表形式返回
d = {1 : "one",2 : "two",3 : "three",4 : "one"}print(d.keys())#输出结果:dict_keys([1, 2, 3, 4])
字典拼接:update() 用一个字典更新另外一个字典
d1={3:"three",2:"two",4:"four",1:"one"}d2={5:"three",6:"two",7:"four",8:"one"}d1.update(d2)print(d1)#输出结果:{3: ‘three‘, 2: ‘two‘, 4: ‘four‘, 1: ‘one‘, 5: ‘three‘, 6: ‘two‘, 7: ‘four‘, 8: ‘one‘}
打印字典所有的项:items()
items将所有的字典项以列表方式返回,列表中项来自(键,值)
d={3:"three",2:"two",4:"four",1:"one"}print(d.items())#输出结果:dict_items([(3, ‘three‘), (2, ‘two‘), (4, ‘four‘), (1, ‘one‘)])
字典初始化:dict.fromkeys()
c=dict.fromkeys([2,3,4],"+++")print(c)#输出结果:{2: ‘+++‘, 3: ‘+++‘, 4: ‘+++‘}
fromkeys函数现象说明:
c=dict.fromkeys([6,7,8],[1,{"a":"A"},"feng"])print(c)c[7][1]["a"]="QQ"print(c)#输出结果:{6: [1, {‘a‘: ‘A‘}, ‘feng‘], 7: [1, {‘a‘: ‘A‘}, ‘feng‘], 8: [1, {‘a‘: ‘A‘}, ‘feng‘]}#输出结果:{6: [1, {‘a‘: ‘QQ‘}, ‘feng‘], 7: [1, {‘a‘: ‘QQ‘}, ‘feng‘], 8: [1, {‘a‘: ‘QQ‘}, ‘feng‘]}
fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None;或者指定默认的对应值:
d1 = dict.fromkeys([1,"two",3])print(d1)d2 = dict.fromkeys([1,"two",3],"cool")print(d2)#输出结果:{1: None, ‘two‘: None, 3: None}#输出结果:{1: ‘cool‘, ‘two‘: ‘cool‘, 3: ‘cool‘}
setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值
d={‘one‘:1,‘two‘:2,‘three‘:3}print(d)print(d.setdefault(‘one‘,1))print(d.setdefault(‘four‘,4))print(d)#输出结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}#输出结果:1#输出结果:4#输出结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘four‘: 4}
删除字典中对应的键:pop()
d={‘one‘:1,‘two‘:2,‘three‘:3}print(d)d.pop(‘one‘)print(d)#输出结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}#输出结果:{‘two‘: 2, ‘three‘: 3}
移除字典中的项:popitem()
d={‘one‘:1,‘two‘:2,‘three‘:3,"four":4}print(d)d.popitem()print(d)#输出结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3, ‘four‘: 4}#输出结果:{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}
Python 字典
评论关闭