4_列表
作者:互联网
列表输出
ary = [1, 2, 3, 4, "是SV"] print(ary) for i in ary: print(i) # 输出列表的最后一个元素 length = len(ary) print(ary[length-1]) print(ary[-1]) # [1, 2, 3] print(ary[:3]) # [1, 2] print(ary[:-3]) # [4, '是SV'] print(ary[3:]) # [1, 2, 3, 4, '是SV'] print(ary[:]) # [1, 3, '是SV'] print(ary[0:5:2]) print(ary[::2]) # ['是SV', 3, 1] print(ary[::-2]) # ['是SV', 4, 3, 2, 1] print(ary[::-1])
列表增删改查
# append、extend增加 # append 每次只能增加一个元素 hero = ['钢铁侠', "绿巨人"] hero.append("黑寡妇") print(hero) # extend 一次可以增加多个元素 hero.extend(["鹰眼", "灭霸", "雷神"]) print(hero) s = [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5, 6] s[len(s):] = [6] print(s) # [1, 2, 3, 4, 5, 6, 7, 8, 9] s[len(s):] = [7, 8, 9] print(s) ---------------------------------- # insert增加元素 s = [1, 3, 4, 5] # [1, 2, 3, 4, 5] s.insert(1, 2) print(s) # [0, 1, 2, 3, 4, 5] s.insert(0, 0) print(s) # [0, 1, 2, 3, 4, 5, 6] s.insert(len(s), 6) print(s) ----------------------------------------- # remove、pop、clear删除列表中的数据 hero = ['钢铁侠', "绿巨人", "黑寡妇", "鹰眼", "灭霸", "雷神"] # ['钢铁侠', '绿巨人', '黑寡妇', '鹰眼', '雷神'] # remove 如果有多个相同的元素,只会删除第一个元素,如果列表没有要删除的元素就会报错 # ['钢铁侠', '绿巨人', '黑寡妇', '鹰眼', '雷神'] hero.remove("灭霸") print(hero) # pop 删除的是对应元素的下标 # ['钢铁侠', '绿巨人', '鹰眼', '雷神'] hero.pop(2) print(hero) # 清除列表的所有元素 hero.clear() print(hero) ------------------------------------------- 替换 # 替换指定元素 # hero = ['钢铁侠', '绿巨人', '黑寡妇', '鹰眼', '雷神'] hero = ['钢铁侠', '绿巨人', '黑寡妇', '鹰眼', '雷神'] hero[4] = "蜘蛛侠" print(hero) # 替换部分元素 # ['钢铁侠', '绿巨人', '黑寡妇', '武松', '林冲', '李逵'] hero[3:] = ["武松", "林冲", "李逵"] print(hero) ------------------------------------------- 查找 # 查找元素出现的次数 # 4 num = [3, 4, 2, 3, 2, 45, 3, 65, 3] print(num.count(3)) # 查找元素的下标 # 1 print(num.index(4)) # 从指定位置开始到指定位置结束查找 # 4 print(num.index(2, 3, 8)) # 将数组的某个元素替换 # [3, 40, 2, 3, 2, 45, 3, 65, 3] num[num.index(4)] = 40 print(num) # 浅拷贝 # [3, 40, 2, 3, 2, 45, 3, 65, 3] num_copy1 = num.copy() print(num_copy1) # [3, 40, 2, 3, 2, 45, 3, 65, 3] num_copy2 = num[:] print(num_copy2) ---------------------------------------
数组排序
# 自动排序 sort(),从小到大排序 # [1, 2, 3, 4, 6, 9, 32, 45, 65] num = [3, 4, 1, 32, 2, 45, 6, 65, 9] num.sort() print(num) # sort(reverse=True) 的效果和先调用sort然后再调reverse的效果一模一样 # [65, 45, 32, 9, 6, 4, 3, 2, 1] num.sort(reverse=True) print(num) # 自动排序 reverse(),只是将数组进行翻转,从大到小排序,前提是数组已经是从小到大排好了 # [9, 65, 6, 45, 2, 32, 1, 4, 3] num1 = [3, 4, 1, 32, 2, 45, 6, 65, 9] num1.reverse() print(num1)
打印二维列表
s = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for i in s: for x in i: print(x, end=" ") print() # 二维数组赋值 # [[1, 1], [1, 1], [1, 1]] A = [2]*3 print(A) for i in range(3): A[i] = [1]*2 print(A)
浅拷贝和深拷贝
# 通过copy拷贝得到两个独立的列表 # [1, 2, 3] x = [1, 2, 3] y = x.copy() x[1] = 1 print(y) # 通过切片拷贝得到两个独立的列表 # [1, 2, 3] x = [1, 2, 3] y = x[:] x[1] = 1 print(y) # 浅拷贝只是拷贝了外层的对象,如果包涵嵌套对象,那么拷贝只是其引用 # [[1, 0, 3], [4, 1, 6], [7, 8, 9]] s = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] y = s.copy() s[0][1] = 0 s[1][1] = 1 print(y) # 深拷贝 # deepcopy函数将原对象拷贝的同时,也将对象中所有引用的子对象一并进行了拷贝 # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] import copy s = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] y = copy.deepcopy(s) s[0][1] = 0 s[1][1] = 1 print(y)
列表推导式
# 将数组元素都乘以2 # [2, 4, 6, 8, 10] od = [1, 2, 3, 4, 5] for i in range(len(od)): od[i] = od[i]*2 print(od) # 用列表推导式效率更高 # [2, 4, 6, 8, 10] od = [1, 2, 3, 4, 5] od = [i * 2 for i in od] print(od) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # for中的循环数据首先会存放在i中,列表中的i+1才是最终存放数据的表达式 x = [i+1 for i in range(10)] print(x) # 取出第二列的元素 # for循环将每一行的元素取出,row[1]每一行的第二个元素,每一行的第二个元素就是第二列元素 # [2, 5, 8] s = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] clo2 = [row[1] for row in s] print(clo2) # 打印对角线的值 # [1, 5, 9] diag = [s[i][i] for i in range(len(s))] print(diag) # [3, 5, 7] diag = [s[i][-i+2] for i in range(len(s))] print(diag) --------------------------------------------- # 先执行for语句,然后再执行if语句,最后执行i+1 # [1, 3, 5, 7, 9] even = [i+1 for i in range(10) if i % 2 == 0] print(even) # 输出f开头的单词 word = ["great", "fish", "brilliant", "excellent", "fantastic"] fword = [w for w in word if w[0] == "f"] print(fword) # 降维 # [1, 2, 3, 4, 5, 6, 7, 8, 9] s = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flatten = [col for row in s for col in row] print(flatten) # ['lL', 'lO', 'lV', 'lE', 'oL', 'oO', 'oV', 'oE', 'vL', 'vO', 'vV', 'vE', 'eL', 'eO', 'eV', 'eE'] a = [x+y for x in "love" for y in "LOVE"] print(a) # [0, 0][0, 3][0, 6][0, 9][2, 0][2, 3][2, 6][2, 9][4, 0][4, 3][4, 6][4, 9][6, 0][6, 3][6, 6][6, 9][8, 0][8, 3][8, 6][8, 9] b = [[x, y] for x in range(10) if x % 2 == 0 for y in range(10) if y % 3 ==0] print(b) # 等价于 for x in range(10): if x % 2 == 0: for y in range(10): if y % 3 == 0: print([x, y], end="") ---------------------------------------------
清除列表中的数据,并非把整个列表清除
# [] s1 = [1, 2, 3, 4, 5] s1.clear() print(s1) s = [1, 2, 3, 4, 5] del s[:] print(s)
标签:hero,ary,元素,列表,num,print 来源: https://www.cnblogs.com/tuyin/p/16552708.html