标签:sof pre pop 存储 class 列表 不可变 microsoft people
字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如:数字、字符串、元组。
字典(dictionary)是除列表意外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
# dic = {‘name‘: ‘jack‘, ‘age‘: ‘18‘, ‘sex‘: ‘male‘} # dic[‘high‘] = 175 #没有键值对,直接添加 # dic[‘age‘] = 20 # 有键值对,则值被覆盖 # dic.setdefault(‘weight‘) #若该键值对存在则不做任何改变,若没有才添加 默认是None # dic.setdefault(‘weight‘, 128) # dic.setdefault(‘name‘, ‘rose‘) #无法实现覆盖 # print(dic)
# print(dic.pop(‘age‘)) #按键删 ,有返回值 返回value # print(dic.pop(‘lucy‘, None)) #可设置返回值,不设置则报错 # print(dic.popitem()) # 随机删除 有返回值 元组 删除的键值 # del dic[‘name‘] #没有返回值 若不存在则报错 # dic.clear() #请空字典 # print(dic)
# dic = {‘name‘: ‘jack‘, ‘age‘: ‘18‘, ‘sex‘: ‘male‘} # dic[‘age‘] = 16 # print(dic) # dic1 = {‘name‘: ‘rose‘, ‘weight‘: 180} # dic1.update(dic) #把dic里面的数据跟新到dic1里面,有键值对的覆盖,没有的增加 # print(dic, dic1)
# dic = {‘name‘: ‘jack‘, ‘age‘: ‘18‘, ‘sex‘: ‘male‘} # print(dic.keys(), type(dic.keys())) #dict_keys([‘name‘, ‘age‘, ‘sex‘]) <class ‘dict_keys‘> # print(dic.values()) #dict_values([‘jack‘, ‘18‘, ‘male‘]) # print(dic.items()) #dict_items([(‘name‘, ‘jack‘), (‘age‘, ‘18‘), (‘sex‘, ‘male‘)]) # for i in dic: # 默认是键 # print(i) # for i in dic.values(): #循环打印值 # print(i) # for i in dic.items(): # print(i) # print(dic[‘name‘]) # print(dic.get(‘age‘)) # print(dic.get(‘high‘, ‘没有这个键‘)) # 防止报错 用get 逼格高
# dic = { # ‘name‘: [‘jack ma‘, ‘rose‘, ‘lucy‘], # ‘sb207‘: { # ‘people_count‘: ‘8‘, # ‘ave_age‘: ‘20‘, # ‘sex‘: ‘male‘, # ‘hobby‘: ‘female‘ # }, # ‘add‘: ‘太原电专‘, # ‘tel‘: 5858888 # } # dic[‘tel‘] = 5656666 # print(dic) # dic[‘name‘].append(‘囡囡‘) # print(dic) # dic[‘name‘][2] = dic[‘name‘][2].upper() # print(dic) # dic[‘sb207‘][‘ave_high‘] = ‘175‘ # print(dic)
标签:sof pre pop 存储 class 列表 不可变 microsoft people
原文地址:https://www.cnblogs.com/-1206-/p/9484512.html