列表与元组
列表是最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
1 >>> names = ['wangeq','zlx','jack','rose']
列表的几种操作方法
1 #通过下标访问列表中的元素,下标从0开始计数 2 >>> #查 3 >>> names[0] 4 'wangeq' 5 >>> names[0:3] 6 ['wangeq', 'zlx', 'jack'] 7 >>> names.index("rose") 8 3 9 >>> names 10 ['wangeq', 'zlx', 'jack', 'rose'] 11 >>> #增 12 >>> names.append('jun') 13 >>> names 14 ['wangeq', 'zlx', 'jack', 'rose', 'jun'] 15 >>> names.insert(2,'li') 16 >>> names 17 ['wangeq', 'zlx', 'li', 'jack', 'rose', 'jun'] 18 >>> #删 19 >>> names.remove('li') 20 >>> names.insert(2,'rose') 21 >>> names 22 ['wangeq', 'zlx', 'rose', 'jack', 'rose', 'jun'] 23 >>> names.remove('zlx') 24 >>> names 25 ['wangeq', 'rose', 'jack', 'rose', 'jun'] 26 >>> names.index('rose') 27 1 28 >>> del names[names.index('rose')] 29 >>> names 30 ['wangeq', 'jack', 'rose', 'jun'] 31 >>> #改 32 >>> names.index('jack') 33 1 34 >>> names[1]= 'JACK' 35 >>> names 36 ['wangeq', 'JACK', 'rose', 'jun'] 37 >>> 38 >>> names[names.index('JACK')] = 'jack' 39 >>> names 40 ['wangeq', 'jack', 'rose', 'jun'] 41 >>> 42 >>> #计数 43 >>> names = ['wangeq','rose','li', 'jack', 'rose', 'jun'] 44 >>> names.count('rose') 45 2 46 >>> #排序 47 >>> names.sort() 48 >>> names 49 ['jack', 'jun', 'li', 'rose', 'rose', 'wangeq'] 50 >>> names.reverse() 51 >>> names 52 ['wangeq', 'rose', 'rose', 'li', 'jun', 'jack'] 53 >>> #扩展 54 >>> names = ['wangeq','rose','li', 'jack', 'rose', 'jun'] 55 >>> names2 = ['wangeq','zhang','wang'] 56 >>> names.extend(names2) 57 >>> names 58 ['wangeq', 'rose', 'li', 'jack', 'rose', 'jun', 'wangeq', 'zhang', 'wang'] 59 >>> #清除列表所有内容 60 >>> names2 61 ['wangeq', 'zhang', 'wang'] 62 >>> names2.clear() 63 >>> names2 64 [] 65 >>> #剔除列表最后一个对象并返回 66 >>> names 67 ['wangeq', 'rose', 'li', 'jack', 'rose', 'jun', 'wangeq', 'zhang', 'wang'] 68 >>> names.pop() 69 'wang' 70 >>> names 71 ['wangeq', 'rose', 'li', 'jack', 'rose', 'jun', 'wangeq', 'zhang']
71 >>> #复制 浅复制
72 >>> names
73 ['wangeq', 'rose', 'li', 'jack', 'rose', 'jun', 'wangeq', 'zhang']
74 >>> names3= names.copy()
75 >>> names
76 ['wangeq', 'rose', 'li', 'jack', 'rose', 'jun', 'wangeq', 'zhang']
77 >>> names3
78 ['wangeq', 'rose', 'li', 'jack', 'rose', 'jun', 'wangeq', 'zhang']
拓展:删除列表中第二个相同的对象(并无意义)
1 names = ['wangeq','li','zhou','jack','li','san']2 first_index = names.index("li") 3 secode_index = names[first_index+1:].index("li") 4 del names[first_index+secode_index+1] 6 print(names)
元组:
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
1 >>> names = ("wang","zhou","zhang","li")
只有两种方法,
计数 count
标记下标 index
字典
字典一种key - value 的数据类型
语法:
1 info = { 2 'stu1101': "wangeq", 3 'stu1102': "zhangsan", 4 'stu1103': "lisi", 5 }
字典特性:
- dict是无序的
- key必须是唯一的,去重
字典功能:
查找
1 info = { 2 "stu1101",{"name":"wangeq","age":22}, 3 "stu1102","zhangsan", 4 "stu1103","lisi" 5 } 6 print(info) 7 {'stu1103': 'alex', 'stu1102': 'jack', 'stu1101': {'name': 'wangeq', 'age': 22}} 8 print(info["stu1101"]["name"]) 9 wangeq 10 print("stu1102" in info) #标准 11 True 12 print(info.get("stu1102")) #获取 13 jack 14 print(info["stu1102"]) #同上 15 jack 16 #print(info["stu1105"]) #key 不存在,KeyError: 'stu1105',get不会报错,会返回"None"
增加
1 info["stu1104"] = ["zhang",32,"abc"] 2 print(info)
3 {'stu1103': 'alex', 'stu1102': 'jack', 'stu1101': {'name': 'wangeq', 'age': 22}, 'stu1104': ['zhang', 32, 'abc']
更新
1 names["stu1104"][0] ="ZHANG" 2 print(names) 3 {'stu1103': 'alex', 'stu1102': 'jack', 'stu1101': {'name': 'wangeq', 'age': 22}, 'stu1104': ['ZHANG', 32, 'abc']}
删除
1 info.pop("stu1105","error") #删除stu1105,如不存在,返回error;标准删除 2 del info["stu1103"] 3 print(info) 4 {'stu1102': 'jack', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'age': 22, 'name': 'wangeq'}} 5 info.popitem() #随机删除
复制
#内置copy函数 浅复制 info2=info.copy() info["stu1102"] = "JACK" print(info) print(info2) {'stu1102': 'JACK', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'age': 22, 'name': 'wangeq'}} {'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'age': 22, 'name': 'wangeq'}, 'stu1102': 'jack'} info["stu1101"]["age"] = 33 print(info) print(info2) {'stu1102': 'JACK', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'age': 33, 'name': 'wangeq'}} {'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'age': 33, 'name': 'wangeq'}, 'stu1102': 'jack'} #深复制 import copy info3 = copy.deepcopy(info) info["stu1102"] = "jack2" print(info) print(info2) {'stu1102': 'jack2', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'name': 'wangeq', 'age': 33}}
{'stu1102': 'JACK', 'stu1101': {'name': 'wangeq', 'age': 33}, 'stu1104': ['ZHANG', 32, 'abc']} info["stu1101"]["age"] = 40 print(info) print(info2) {'stu1102': 'jack2', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'name': 'wangeq', 'age': 40}} {'stu1102': 'JACK', 'stu1101': {'name': 'wangeq', 'age': 33}, 'stu1104': ['ZHANG', 32, 'abc']}
其它功能
#values print(info.values()) dict_values([['ZHANG', 32, 'abc'], 'jack2', {'name': 'wangeq', 'age': 40}]) #keys print(info.keys()) dict_keys(['stu1102', 'stu1104', 'stu1101']) #setdefault #dict.setdefault(key, default=None) print(info) {'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'name': 'wangeq', 'age': 40}, 'stu1102': 'jack2'} print(info.setdefault("stu1106","zhouwu")) #键不存在,添加并返回默认值 zhouwu print(info) {'stu1106': 'zhouwu', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'name': 'wangeq', 'age': 40}, 'stu1102': 'jack2'} print(info.setdefault("stu1102")) #键存在,返回stu1102 values值 jack2 print(info) {'stu1106': 'zhouwu', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'name': 'wangeq', 'age': 40}, 'stu1102': 'jack2'} #update b = {"stu1102":"JACK"} print(b) {'stu1102': 'JACK'} info.update(b) print(info) {'stu1106': 'zhouwu', 'stu1104': ['ZHANG', 32, 'abc'], 'stu1101': {'name': 'wangeq', 'age': 40}, 'stu1102': 'JACK'} #items print(info.items()) dict_items([('stu1101', {'name': 'wangeq', 'age': 40}), ('stu1104', ['ZHANG', 32, 'abc']), ('stu1106', 'zhouwu'), ('stu1102', 'JACK')]) #循环dict #one for key in info: print(key,info[key]) #two for k,v in info.items(): #会先把dict转成list,数据里大时莫用 print(k,v) #通过列表生成默认dict info4 =dict.fromkeys([1,2,3],'test') print(info4) {1: 'test', 2: 'test', 3: 'test'}