1, 判断是不是列表
li = ['lis3a', 'mary', 'lucy', 'hh', 'kk', 'gg', 'mm', 'oo', 'vv'] if type(li) == list: print("list")
2,字典的key必须是不可变数据类型,也就是bool,int,str,tuple,value可以是任意的数据类型,有点二分查找去查询,存储大量的键值对,特点是无序的,3.5以前版本
3,字典的增加与修改方式,没有的添加,有的覆盖
dict1 = {"name":"lisa","age":"22","hobby":"piano"} dict1["height"]="165" #没有的添加进去 dict1["name"]="mary" #有的覆盖,就相当于修改了 print(dict1) 运行结果: {'name': 'mary', 'age': '22', 'hobby': 'piano', 'height': '165'}
4,添加的另外一种方式setdefault
dict1 = {"name":"lisa","age":"22","hobby":"piano"} dict1.setdefault("hometown") # 不设置value默认为None print(dict1) dict1.setdefault("name") # 本来有的不会更新 print(dict1) dict1.setdefault("gender","female") # 设置了value print(dict1) dict1.setdefault(5,True) # 支持很多数据类型,详见上一条 print(dict1)
5,删除,pop函数,必须制定key,不指定会报错,制定成value也会报错,制定的不存在的key 也会崩溃
dict1 = {"name":"lisa","age":"22","hobby":"piano"} print(dict1.pop()) 运行结果: Traceback (most recent call last): File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/5.1_dict.py", line 28, in <module> print(dict1.pop()) TypeError: pop expected at least 1 arguments, got 0 dict1 = {"name":"lisa","age":"22","hobby":"piano"} print(dict1.pop(“name”)) 运行结果: lisa
dict1 = {"name":"lisa","age":"22","hobby":"piano"} print(dict1.pop("gender")) 运行结果: Traceback (most recent call last): File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/5.1_dict.py", line 29, in <module> print(dict1.pop("gender")) KeyError: 'gender'
dict1 = {"name":"lisa","age":"22","hobby":"piano"} print(dict1.pop("gender",None)) # 这个可以专门用来表示没有这个key时候的返回值,或者提示“没有此键”,等也用来防止程序崩溃 运行结果: None
6,删除的另外一种popitem ,不需要参数,3.5以前随机删除,3.5以后是最后一个
7,del函数可以删除摸一个,或者删除整个dict,这个和列表类似
8,清空字典,clear函数,这个也和字典类似
9,改,update函数
dict1 = {"name":"lisa","age":"22","hobby":"piano"} dict2 ={'name':"mary","gender":"female"} dict2.update(dict1) print(dict1) print(dict2) #用dict1来更新dict2,没有的添加,有的更新value 运行结果: {'name': 'lisa', 'age': '22', 'hobby': 'piano'} {'name': 'lisa', 'gender': 'female', 'age': '22', 'hobby': 'piano'}
10,查,区分,value,key,item
dict1 = {"name":"lisa","age":"22","hobby":"piano"} print(dict1.keys()) print(dict1.values()) print(dict1.items()) 运行结果: dict_keys(['name', 'age', 'hobby']) dict_values(['lisa', '22', 'piano']) dict_items([('name', 'lisa'), ('age', '22'), ('hobby', 'piano')])
dict1 = {"name":"lisa","age":"22","hobby":"piano"} for i in dict1: # 默认输出的是key和下一句效果是一样的 print(i) for i in dict1.keys(): print(i) 运行结果: name age hobby name age hobby
11,穿插点字典以外的知识点:
a,b,c =1,2,3 print(a,b,c) a,b = b,a print(a,b,c) 运行结果: 1 2 3 2 1 3
a,b = [1,2] print(a,b) a,b =[1,2],[3,4] print(a,b) # 永远按照最小的单位进行拆包 运行结果: 1 2 [1, 2] [3, 4]
12,iterm打印出来的是元祖,可以进行拆包赋值给k,v
dict1 = {"name":"lisa","age":"22","hobby":"piano"} for i in dict1.values(): print(i) for i in dict1.items(): print(i) for k,v in dict1.items(): print(k,v) 运行结果: lisa 22 piano ('name', 'lisa') ('age', '22') ('hobby', 'piano') name lisa age 22 hobby piano
13,查,两种,首选get函数,因为不存在是不会崩溃
v1 = dict1.get("name") print(v1) v1 = dict1.get("name1") print(v1) v1 =dict1.get("name1","没有此键") print(v1) 运行结果: lisa None 没有此键
dict1 = {"name":"lisa","age":"22","hobby":"piano"} v1 =dict1["name"] print(v1) 运行结果: lisa dict1 = {"name":"lisa","age":"22","hobby":"piano"} v1 =dict1["name1"] print(v1) 运行结果: Traceback (most recent call last): File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/5.1_dict.py", line 82, in <module> v1 =dict1["name1"] KeyError: 'name1'
14,一道练习题
str1 ="hiohph65nooihoh89nlohiolh76nklnl4" # 找出里面有多少个数字,65算一个,89算一个 for i in str1: if i.isdigit(): break else: str1 = str1.replace(i," ") #用空格替换非数字,这里一定要把新生成的字符串赋值回去 l = str1.split() print(len(l)) 运行结果: 4