dict-字典功能介绍
叨逼叨:
#字典 可变类型 意思就是修改的是自己本身
#可变类型,当修改后,内存里的值也会对应着修改
#不可变类型,当修改后,会在内存里开启一块新的空间,放新的值
#1.清空
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # name.clear() # print(name)
def clear(self): # real signature unknown; restored from __doc__ """ D.clear() -> None. Remove all items from D. """ pass
#2.拷贝复制 浅拷贝
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.copy() # print(v) # print(name)
def copy(self): # real signature unknown; restored from __doc__ """ D.copy() -> a shallow copy of D """ pass
#3.根据key 取value的值
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.get('name') #这个方式以后会常用 没有取值不报错 # v1 = name['name'] #没有取到 会报错 # print(v) # print(v1)
def get(self, k, d=None): # real signature unknown; restored from __doc__ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass
#4.取出key和value
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.items() # print(v) # #打印结果: # #dict_items([('name', 'alex'), ('age', 23), ('gender', '女')]) # for item in name.items(): # print(item) # #打印结果 # # ('name', 'alex') # # ('age', 23) # # ('gender', '女') # for key,value in name.items(): # print(key,value) # #打印结果 # #name alex # #age 23 # #gender
def items(self): # real signature unknown; restored from __doc__ """ D.items() -> a set-like object providing a view on D's items """ pass
#5.单独取出key
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.keys() # print(v) # #打印结果 # #dict_keys(['name', 'age', 'gender']) # for i in name.keys(): # print(i) # #打印结果 # #name # #age # #gender
def keys(self): # real signature unknown; restored from __doc__ """ D.keys() -> a set-like object providing a view on D's keys """ pass
#6.单独取出value
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.values() # print(v) # for i in name.values(): # print(i) # #打印结果分别如下 # # dict_values(['alex', 23, '女']) # # alex # # 23 # # 女
def values(self): # real signature unknown; restored from __doc__ """ D.values() -> an object providing a view on D's values """ pass
#7.删除并获取对应的value值
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.pop('name') # print(v) # print(name) #还可以这么删除 # v = { # 'name':'alex', # 'password':'123', # 'gender':'girl' # } # del v['name'] # print(v)
def pop(self, k, d=None): # real signature unknown; restored from __doc__ """ 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 """ pass
#8.随机删除键值对,并获取到删除的键值对
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = name.popitem() # print(v) # print(name)
def popitem(self): # real signature unknown; restored from __doc__ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass
#9.增加,如果存在,不做操作
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # #name.setdefault('name','alex') # name.setdefault('love','none') # print(name)
def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ pass
#10.批量增加或修改
# name = { # 'name':'alex', # 'age':23, # 'gender':'女' # } # v = { # 'k1':'v1', # 'k2':'v2' # } # #name.update({'love':'none','aihao':'sleep'}) # name.update(v) # print(name)
def update(self, E=None, **F): # known special case of dict.update """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] """ pass
#11.接受一个可循环的东西,创建字典
#@staticmethod 脑袋上顶了这个的,就比较特殊,要使用dict.调用 静态方法 # dic = dict.fromkeys(['k1','k2','k3'],123) # print(dic) # #打印结果 # #{'k1': 123, 'k2': 123, 'k3': 123}#创建了一个字典,value都一样,也只能一样 # dic['k1'] = 'alex' # print(dic) # #打印结果 # #{'k1': 'alex', 'k2': 123, 'k3': 123} # # dic1 = dict.fromkeys(['k1','k2','k3'],[1,]) # print(dic1) # #打印结果 # #{'k1': [1], 'k2': [1], 'k3': [1]} # dic1['k1'].append('222') # print(dic1) # #打印结果 # #{'k1': [1, '222'], 'k2': [1, '222'], 'k3': [1, '222']} # dic1['k1'].remove('222') # print(dic1) # #打印结果 # #{'k1': [1], 'k2': [1], 'k3': [1]}
# #观察打印结果,配合着下图理解
@staticmethod # known case def fromkeys(*args, **kwargs): # real signature unknown """ Returns a new dict with keys from iterable and values equal to value. """ pass
#12.相互嵌套
# students_info = { # '张三': '好学生', # '李四': {'English_name': 'lisi', 'age': 28, 'gender': 'boy'}, # '王五': {'alse': 'wangwu', 'age': 52, '后宫': ['甄嬛', '华妃', '皇后']} # } # print(students_info) #相互嵌套的添加内容 # 把列表和字典单独的基本操作熟练了,这个混合的就好处理些 # students_info = { # '张三': '好学生', # '李四': {'English_name': 'lisi', 'age': 28, 'gender': 'boy'}, # '王五': {'alse': 'wangwu', 'age': 52, '后宫': ['甄嬛', '华妃', '皇后']}, # '赵六': ['alex', 'eric', ['a', 'b', 'c'], '李杰', {'k11': 'vv1'}], # } # 增加 #students_info['赵六'][2].append('d') # print(students_info) # 修改 # students_info['赵六'][4]['age'] = '我没有年龄' # print(students_info) #实例: # user_list = [ # {'name': 'alex', 'password': '123'}, # {'name': 'blex', 'password': '123'}, # {'name': 'clex', 'password': '123'} # ] # print(user_list) # user = input('用户名:') # pwd = input('密码:') # for item in user_list: # if user == item['name'] and pwd == item['password']: # print('登录成功!') # break # else: # print("滚!!!")
##13.额外重点
#字典的key
# 1.是不可变类型 元组是不可变类型 元组里套列表就不行了,列表是可变类型
# 2. TRUE和1 可以做key 但是每次选其一 否则会重复
# dic = { # 'k1': 'v1', # 'k2': [1,2,3,], # (1,2): 'lllll', # 1: 'fffffffff', # 111: 'asdf', # } # print(dic)