字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如:数字、字符串、元组。
字典(dictionary)是除列表意外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
1,字典无序。2,数据关联性强,3键值对。唯一一个映射的数据类型。
dic = {'name':'jinxin','age':20} dic1 = {'py8期':['晓梅','方胜君',],'py6期':['zhangsan','lisi','wangwu']} print(dic['name']) print(dic1['py8期']) for i in dic: print(i) 输出: jinxin ['晓梅', '方胜君'] name age
字典的键必须是可哈希的(不可变的数据类型:字符串,数字,布尔值,元祖)并且是唯一的
不可哈希的(可变的数据类型:列表,字典,set)
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} print(dic) 输出: {'name': 'alex', 'age': 56, 'hobby ': 'oldwomem'}
#增:
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} dic['weight'] = 75 print(dic) 输出: {'name': 'alex', 'age': 56, 'hobby ': 'oldwomem', 'weight': 75}
# setdefault 在字典中添加键值对,如果只有键那对应的值是none,但是如果原字典中存在设置的键值对,则他不会更改或者覆盖。
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} dic.setdefault('k') dic.setdefault('name','v') print(dic) 输出: {'name': 'alex', 'age': 56, 'hobby ': 'oldwomem', 'k': None}
删:
pop
pop根据key删除键值对,并返回对应的值,如果没有key则返回默认返回值
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} dic.pop('name') print(dic) dic = {'name':'alex','age':56,'hobby ':'oldwomem'} print(dic.pop('name')) print(dic) 输出: {'age': 56, 'hobby ': 'oldwomem'} alex {'age': 56, 'hobby ': 'oldwomem'}
popitem:
随机删除字典中的某个键值对,将删除的键值对以元祖的形式返回
dic_pop1 = dic.popitem() # 随机删除字典中的某个键值对,将删除的键值对以元祖的形式返回
print(dic_pop1) # ('name','jin')
clear: 清空字典
dic_clear = dic.clear() # 清空字典 print(dic,dic_clear) # {} None
del:删除
# del dic["name"] # 没有返回值。 # print(dic)
改:
dic[key] = new_value
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} dic['name'] = '日天' print(dic) 输出: {'name': '日天', 'age': 56, 'hobby ': 'oldwomem'}
# dic = {"name":"jin","age":18,"sex":"male"} # dic2 = {"name":"alex","weight":75} # dic2.update(dic) # 将dic所有的键值对覆盖添加(相同的覆盖,没有的添加)到dic2中 # print(dic2)
输出:
{'name': 'jin', 'weight': 75, 'age': 18, 'sex': 'male'}
查:
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} print(dic['name']) # print(dic['gfgfgfdgf']) # 报错 print(dic.get('name')) print(dic.get('gfdgfgf')) # None print(dic.get('gfdgfgf','sb,没有这个键')) print(dic.keys(),type(dic.keys())) 输出: alex alex None sb,没有这个键 dict_keys(['name', 'age', 'hobby ']) <class 'dict_keys'>
其他操作:
dic = {'name':'alex','age':56,'hobby ':'oldwomem'} item = dic.items() print(item,type(item)) #dict_items([('name', 'alex'), ('age', 56), ('hobby ', 'oldwomem')]) <class 'dict_items'> # 这个类型就是dict_items类型,可迭代的 keys = dic.keys() print(keys,type(keys)) # dict_keys(['name', 'age', 'hobby ']) <class 'dict_keys'> values = dic.values() print(values,type(values)) # dict_values(['alex', 56, 'oldwomem']) <class 'dict_values'>
循环:
dic = {"name":"jin","age":18,"sex":"male"} for key in dic: print(key) for item in dic.items(): print(item) for key,value in dic.items(): print(key,value) 输出: name age sex ('name', 'jin') ('age', 18) ('sex', 'male') name jin age 18 sex male
dic = {"name":"jin","age":18,"sex":"male"} for key in dic.keys(): print(key) for i in dic: print(i) 输出: name age sex name age sex
dic = {"name":"jin","age":18,"sex":"male"} print(dic.values()) for value in dic.values(): print(value) 输出: dict_values(['jin', 18, 'male']) jin 18 male
a,b = [1,2] print(a,b) 输出: 1 2