1、列表
列表是可变的, 有序的数据类型,列表是按照添加顺序来保存的,可以存放各种数据类型.
1.1 列表的切片(同字符串)
1.2 列表的增删改查
注意: 列表是可以直接在列表上面进行操作的,不同于字符串
增:
append() ----> 添加在列表末
insert(索引值,"添加内容" ) ----> 添加在列表的指定位置
extend() ----> 迭代添加,把内容挨个添加到原列表中
lst = ["a","b","c","d"]
lst2 = [1,2,3]
lst.append(lst2) ----> lst为["a","b","c","d",[1,2,3]]
lst.extend(lst2) ----> lst为["a","b","c","d",1,2,3]
删:
pop(5) ---->删除索引为5的元素(不指定则默认删除最后一个元素).
pop()是有返回值的,返回值是被删除的元素.
remove("被删除内容") ---->删除指定元素.
del lst[ ] ---->切片删除
lst = [11,22,33,44,55]
del lst[3:] ----> lst = [11.22.33]
lst.clean() ---->清空列表,只剩一个[ ]
改:
lst = ["a","b","c","d","e","f","g",]
lst[2] = "C" ----> ["a","b","C","d","e","f","g",]
lst[1:5] = "BCD" ----> ["a","BCD","f","g",]
lst[1:4:2] = "B","D" ----> ["a","B","c","D","e","f","g",] 如果有步长的话要注意元素的个数是否对得上.
add1 = "我爱Python"
add2 = ["我爱Python"]
lst[2:4] = add1 ----> lst = ["a","b","我","爱",,"P","y","t","h","o","n","e","f","g",]
lst[2:4] = add2 ----> lst = ["a","b","我爱Python","e","f","g",]
查:
for循环进行遍历.
其他操作:
count("查找内容") ----> ...元素的个数
sort() ----> 对列表元素进行排序. sort 只对列表有用,对元组,集合,字典不能操作.
sort(reverse=True) ---->排序完后再反序.
sorted(列表) ----> 对列表元素进行排序. sorted 对元组,集合,字典都能操作,作用于字典时只对键进行排序.
reverse() ----> 对列表反序操作.
1.3 列表的嵌套
总结: 降维操作, 层层解析
2、元组
注意: 元组是不可变类型.
元组内部的第一层元素的内存地址是固定不变的.
tp = ("a","b",1,2,["lol","dnf"],("baoleizhiye","cs"))
tp记录的是第一层元素(字符串,数字,列表,元组)的内存地址, 虽然也记录了列表的内存地址,但是列表内部的元素是可变的,所以总结起来:
元组的不可变意思是子元素不可变,但子元素的子元素可变与否取决于它是不是一个可变的数据类型.
元组在只有一个元素时要在元素末尾加",",不然就是数字.
(2) ----> int 2
(2,) ----> tuple(2.)
3、range()函数
for i in range(100) -----> [0,99]
for i in range(1,100) -----> [1,99]
for i in range(1,100,2) -----> [1,3,5,7,9,11......97,99]