一.列表
温馨提示:对图片点右键——在新标签页中打开图片:
1.什么叫列表
列表是一个可变的数据类型,可以由[ ]表示,每一项元素使用逗号隔开,列表什么都可以装进去,可以装对象的对象.例如:;lst = ["马化腾‘,"王健林","小川",[1,2,3],["天龙八部",[斗破苍穹],"大神"],1].可以装大量数据.
2.列表的索引和切片
列表和字符串一样,也有索引和切片.只不过列表的切片切出来的内容是列表.
. [开始位置:结束位置:步长],顾头不顾尾
索引的下标从0开始
例1:
lst = ["天龙八部","斗破苍穹","地下城",[110,119,120,["人民币","美元"],100],"风"] print(lst[2]) print(lst[3][3][1]) lst1 = ["天龙八部","斗破苍穹","地下城","异人夜","泡泡堂","跑跑"] print(lst1[1:3]) print(lst1[-3:-1]) print(lst1[-2:-6:-1]) 答案: 地下城 美元 [‘斗破苍穹‘, ‘地下城‘] [‘异人夜‘, ‘泡泡堂‘] [‘泡泡堂‘, ‘异人夜‘, ‘地下城‘, ‘斗破苍穹‘]
3.列表的增删改查*********
㈠.增加;
注意:lst和str是不同的,lst可以发生改变,所以就在原来的对象上进行了操作.
①.append():追加,在列表最后的位置在加进去(在屁股后面添加)
注意:append每次只能增加一个元素.
例1:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] lst.append("许家印") print(lst) 答案: [‘马化腾‘, ‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘, ‘许家印‘]
②.insert(index,元素):在index位置添加元素(在xxx位置插入xxx内容)
例2:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] lst.insert(1,"向华强") print(lst) 结果: [‘马化腾‘, ‘向华强‘, ‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘]
③.extend():迭代添加
注意:可以增加多个元素
例3:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] lst.extend("许家印") # 注意 print(lst) 结果: [‘马化腾‘, ‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘, ‘许‘, ‘家‘, ‘印‘] lst = ["马化腾","马云","王健林","异人夜","刘强东"] lst.extend(["许家印","向华强","雷军"]) print(lst) 结果; [‘马化腾‘, ‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘, ‘许家印‘, ‘向华强‘, ‘雷军‘]
㈡.删除
①.pop(index):按照位置删除元素.
列1;
lst = ["马化腾","马云","王健林","异人夜","刘强东"] n = lst.pop(0) #返回被删除的数据 print(n) print(lst) 结果: 马化腾 [‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘]
②.remove(元素):直接删除元素.
注意:如果列表中没有要删除的元素,编程会报错
例2:
lst.remove("马化腾") print(lst) 结果: [‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘]
③.del lst:切片删除
例3:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] del lst[1:3] print(lst) 结果: [‘马化腾‘, ‘异人夜‘, ‘刘强东‘]
④.clear():清空列表
例4:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] lst.clear() print(lst) 结果: []
㈢.修改
索引修改和切片修改.
例:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] lst[0] = "许家印" print(lst) lst1 = ["马化腾","马云","王健林","异人夜","刘强东"] lst1[1:3] = ["向华强"] #先删除后添加 print(lst1) lst2 = ["马化腾","马云","王健林","异人夜","刘强东"] lst2[:3:2] = ["向华强","许家印"] #切片修改的时候,如果步长不是1,注意元素个数. print(lst2) 结果: [‘许家印‘, ‘马云‘, ‘王健林‘, ‘异人夜‘, ‘刘强东‘] [‘马化腾‘, ‘向华强‘, ‘异人夜‘, ‘刘强东‘] [‘向华强‘, ‘马云‘, ‘许家印‘, ‘异人夜‘, ‘刘强东‘]
㈣.查询:列表时一个科跌代对象,所以可以进行for循环.
语法: for el in lst:
print(el)
例:
lst = ["马化腾","马云","王健林","异人夜","刘强东"] for el in lst: #element(元素) print(el) 结果: 马化腾 马云 王健林 异人夜 刘强东
㈤.常用操作
①.sort():排序,reverse=True降序
例1:
lst = [1,5,8,99,48,156,3,24,56] lst.sort() print(lst) lst.sort(reverse=True) print(lst) 结果; [1, 3, 5, 8, 24, 48, 56, 99, 156] [156, 99, 56, 48, 24, 8, 5, 3, 1]
②.reverse():翻转
例2:
lst = ["马云","马化腾","刘强东","雷军","异人夜"] lst.reverse() print(lst) 结果: [‘异人夜‘, ‘雷军‘, ‘刘强东‘, ‘马化腾‘, ‘马云‘]
③.len():求长度
例3:
lst = ["马云","马化腾","刘强东","雷军","异人夜"] print(len(lst)) 结果: 5
4.列表的嵌套
采用降维操作,一层一层的看就好
例
lst = ["马云","马化腾","雷军","异人夜",["川普","pujing","金三胖",[1,2,3,4],"酋长"],"刘强东"] print(lst[2]) #找到雷军 print(lst[4][3][1]) #找到2 lst[4][1] = lst[4][1].upper() #将pujing变成大写,在放回去 print(lst) lst[4][0] = lst[4][0].replace("川普","特朗普") print(lst) #把川普换成特朗普 结果: 雷军 2 [‘马云‘, ‘马化腾‘, ‘雷军‘, ‘异人夜‘, [‘川普‘, ‘PUJING‘, ‘金三胖‘, [1, 2, 3, 4], ‘酋长‘], ‘刘强东‘] [‘马云‘, ‘马化腾‘, ‘雷军‘, ‘异人夜‘, [‘特朗普‘, ‘pujing‘, ‘金三胖‘, [1, 2, 3, 4], ‘酋长‘], ‘刘强东‘]
二.元组
元组俗称不可变的列表, 用( )表示,里面可以放任何类型的数据,可以查询,循环,切片.但是不可以改变.
如果元组只有一个元素(元素,)
tuple(3,)
空元组:tuple()
元组是一个可迭代对象,可使用for循环.
注意:元组不可以修改和删除,但是索引可以
例1:
tu = ("马云","马化腾","雷军","异人夜") tu[0] = "呵呵" print(tu) 结果: File "E:/lx/00003.py", line 58, in <module> tu[0] = "呵呵" TypeError: ‘tuple‘ object does not support item assignment
tu = ("马云","马化腾","雷军","异人夜") print(tu[2]) print(tu[::2]) #切片之后还是元组 结果: 雷军 (‘马云‘, ‘雷军‘)
注意:元组的不可变的意思是子元素不可变,但是子元素内部的子元素是可以改变的,这取决于子元素是不是可变的对象.
例:
tu = (1, "哈喽", "how are you?", "alex", ["alex"]) tu[3].upper() #不能改变,结果返回原元组 print(tu) 结果: (1, ‘哈喽‘, ‘how are you?‘, ‘alex‘, [‘alex‘])
tu = ("马云","马化腾","雷军","异人夜",["川普","pujing","金三胖",[1,2,3,4],"酋长"],"刘强东") tu[4][1] = tu[4][1].upper() print(tu) 结果: (‘马云‘, ‘马化腾‘, ‘雷军‘, ‘异人夜‘, [‘川普‘, ‘PUJING‘, ‘金三胖‘, [1, 2, 3, 4], ‘酋长‘], ‘刘强东‘)
三.range:
1. 使用range和for循环来获取列表中的索引.
for i in range(len(列表)):
i : 索引
列表[i] 元素
例:
lst = ("马云","马化腾","雷军","异人夜") #获取到列表的索引,拿到索引后,可以拿到元素. for i in range(len(lst)): print(i) #i就是lst的索引 print(lst[i]) 结果: 1 马化腾 2 雷军 3 异人夜
2.range可以进行切片
① .range(n) 从0到n-1
for i in range(10):
print(i)
②.range(m,n) 从m到n-1
③.range(m,n,q) 从m到n-1,每q个取1个
.
原文地址:https://www.cnblogs.com/zmc940317/p/9397854.html