一、列表:
1、列表概述:
列表是可变的,可以修改内容。例如:li = [1,23,34,"adf","name"]
1、列表中的元素可以是数据,字符串,列表,布尔值等任何类型序列。列表可以视为一个集合,其中可以放置任意类型序列。
2、元素之间使用逗号分割开来
2、列表取值操作:3种方法,索引,切片,for循环
01.索引取值:
li = [1, 23,34,"adf",["asa","fgw"],"name",True] print(li[2])
02. 切片取值:结果通常为列表
li = [1, 23,34,"adf",["asa","fgw"],"name",True] print(li[3:5])
03. for循环:列表中元素依次输出
li = [1, 23,34,"adf",["asa","fgw"],"name",True] for item in li: print(item)
3、列表修改操作:列表元素可以被修改,通过索引和切片可以完成修改操作。
01.索引修改:
li = [1, 23,34,"adf",["asa","fgw"],"name",True] li[1] = [1,2,3,56] print(li)
索引删除:
li = [1, 23,34,"adf",["asa","fgw"],"name",True] del li[1] print(li)
02. 切片修改和删除:可以同时修改多个元素
li = [1, 23,34,"adf",["asa","fgw"],"name",True] li[1:3] = [120,30] print(li)
切片删除:
li = [1, 23,34,"adf",["asa","fgw"],"name",True] del li[2:4] print(li)
4、in操作:
li = [1, 23,34,"adf",["asa","fgw"],"name",True] v = 34 in li print(v)
5、特定元素取值:
li = [1, 23,34,"adf",["asa",[3,6,3],"fgw"],"name",True] v = li[4][1][0] print(v)
6、字符串与列表间的相互转换:
01.字符串转换成列表:
s = "afkjahgalflajkfdhfa" v = list(s) print(v)
02.列表转换成字符串: 使用for循环实现(既有数字,又有字符串)
li = [11,22,44,"alex",34,"welcome"] s = "" for i in li: s = s + str(i) print(s)
如果列表中元素均为字符串,可以使用join实现:
li = ["my","name","is","alex","welcome"] s = " " v = s.join(li) print(v)
7、列表方法:
01. append:将一个对象附加到列表末尾。
li = ["my","name","is","alex","welcome"] li.append("my house") print(li)
02. clear:清空列表。
li = ["my","name","is","alex","welcome"] li.clear() print(li)
03、copy:复制列表
li = ["my","name","is","alex","welcome"] v = li.copy() print(li)
04、count:计算指定的元素在列表中出现的次数
li = ["to", "be","or","not","to", "be"] v = li.count("be") print(v)
05、extend:能够同时将多个值附加在列表末尾。或者,可以使用一个列表来扩展另一个列表。
li = [11,22,33,22,44] li.extend([9898,"asdafa"]) print(li)
列表扩展:
li = [11,22,33,22,44] b = ["we", "are","the","champion"] li.extend(b) print(li)
06、index:查找指定值第一次出现的索引。
li = [11,22,33,22,44] v = li.index(22) print(v)
07、insert:将一个对象插入列表。
li = [11,22,33,22,44] li.insert(0,99) print(li)
08、pop:从列表中删除一个元素,通常默认为最后一个元素,并返回这一元素。也可以删除指定索引位置的数值,并将该值返回。
li = [11,22,33,22,44] v = li.pop(1) print(li) print(v)
09、remove:删除第一个为指定值的元素
li = ["to", "be","or","not","to", "be"] v = li.count("be") print(v)
10、reverse:按照相反的顺序排列列表中的元素。
li = [1,2,3,4,5,6] li.reverse() print(li)
11、sort:就地排序
li = [11,44,22,33,22] li.sort() print(li)
相反顺序排列:
li = [11,44,22,33,22] li.sort(reverse=True) print(li)
二、元组:
1、元组概述:
元组也是序列,不同于列表的是,元组不能修改,使用逗号将元素隔开。
01、索引:
tu = (111,"alex",(11,22),22,33,4) v = tu[2] print(v)
02、切片:
tu = (111,"alex",(11,22),22,33,4) v = tu[0:2] print(v)
03、可以被for循环,可迭代对象:
tu = (111,"alex",(11,22),22,33,4) for item in tu: print(item)
04、字符串,列表,元组的相互转换:
s = "asdfegsa" li = [11,22,33,44,55] tu = (123,456,"ert") #字符串转换成元组 v1 = tuple(s) print(v1) #列表转换成元组 v2 = tuple(li) print(v2) #元组转换成列表 v3 = list(tu) print(v3)
05、元组元素的取出:
tu = (111,"alex",(11,22),[(33,44)],22,33,4) v = tu[3][0][0] print(v)
06、********元组的一级元素不可被修改、删除、增加********* 二级可以被修改
tu = (111,"alex",(11,22),[(33,44)],22,33,4) tu[3][0] = 567 print(tu)
三、字典:
1、字典概述:
字典由键及其相对应的值组成,这种键-值对称为项。键与值之间使用:隔开。项之间使用,隔开,整个字典放置于{}内。(需要掌握get,update,items,keys,values功能)
形式:
dict:
info = { "k1":"v2", #键值对 "k2":"v2" }
2、***字典的value可以是任何值*********
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } print(info)
字典是无序的
3、********列表,字典不能做字典的key*******
info = { 1:'asdf', "k1": 'asff', "k1": 'asff', True: "123", (11,22): 123, } print(info)
True----1 False----0
4、索引取值操作:(不能通过切片操作取值)
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } v = info['k1'] print(v)
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } v = info['k3'][3]['kk3'][0] print(v)
5、删除操作:
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } del info['k1'] print(info)
6、字典支持for循环:
1==================输出只有key
for item in info:
print(item)
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } for item in info: print(item)
2==================输出key与value(较为复杂)
for item in info.keys():
print(item,info[item])
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } for item in info.keys(): print(item,info[item])
3==================输出key与value(高效,简介)
for k,v in info.items():
print(k,v)
info = { "k1": 18, "k2": True, "k3": [11,22,33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11,22)}], "k4": (11,22,33,44) } for k,v in info.items(): print(k,v)
4. 字典功能:
01. update:
dic = { "k1": 'v1', "k2": 'v2', } dic.update({'k1': '11111','k2': 123}) print(dic)
02. get: 根据key获取值,key不存在时,可指定默认值
dic = { "k1": 'v1', "k2": 'v2', } v1 = dic.get('k1',1111) v2 = dic.get('k3',1111) print(v1) print(v2)
内容总结:
1、数字
int()
2、字符串
replace,find,join,split,strip,startwith,upper,lower,format
template = "i am {name}, age: {age}"
v = template.format(name='alex',age=19)
v = template.format(**{"name":'alex',"age":19})
print(v)
3、列表
append,extend,insert
索引,切片,循环
4、元组
不需要记忆方法
索引,切片,循环 一级元素不能被修改删除
5、字典
get,update,keys,values,items
for循环,索引
dic = {
"k1":'v1'
}
v1 = "k1" in dic
v2 = "v1" in dic.values()
print(v1)
print(v2)
6、布尔值
0 1
bool()
None "" () [] {} 0==> False
其它都是True
小结:
数字,字符串,列表,元组,字典。
可变的:列表,
不可变的:元组,字符串,数字
1、访问顺序:字符串,列表,元组
2、映射:字典
3、直接访问:数字
存放元素个数:
容器类型:列表,元组,字典
原子:数字,字符串