list 学习第一课
list_one = ["你","是","我","未","来","的","人"] # append() list_one .append("啊") print(list_one ) #['你', '是', '我', '未', '来', '的', '人', '啊'] # insert() list_one .insert(1,"不") print(list_one ) #['你', '不', '是', '我', '未', '来', '的', '人', '啊'] # extend() a = [1,2,3] b = [4,5,6] a.extend(b) print(a) #[1, 2, 3, 4, 5, 6]
list_one = ["你","不","是","我","未","来","的","人"] # remove() 根据元素的值进行删除 list_one .remove("不") print(list_one ) #['你', '是', '我', '未', '来', '的', '人'] # pop() 根据索引进行删除 list_one = ["你","不","是","我","未","来","的","人"] list_one .pop(1) print(list_one ) #['你', '是', '我', '未', '来', '的', '人'] #del 暴力删除 list_one = ["你","不","是","我","未","来","的","人"] del list_one [1] print(list_one ) #['你', '是', '我', '未', '来', '的', '人'] del list_one [0:3] print(list_one ) #['未', '来', '的', '人'] #del list_one #删除整个列表 #print(list_one ) # NameError: name 'list_one' is not defined
list_two = ["人","生","苦","短","我","用","Python"] list_two [6] = "C语言" print(list_two ) #['人', '生', '苦', '短', '我', '用', 'C语言'] list_two = ["人","生","苦","短","我","用","Python"] list_two [2:4] = ["很","甜"] print(list_two ) #['人', '生', '很', '甜', '我', '用', 'Python']
list_three = [1,2,3,4,5,6,1,1,2,3,1,1,3,4,5] a = list_three .count(1) print(a) # 5
list_four = ["和","睦","家","庭"] b = list_four .index("家") print(b) # 2
list_five = [1,2,3,4] c = sum(list_five ) print(c) # 10
# list_name.sort(key=None,reverse=False) list_six = [1,3,5,2,4,7,8] list_six .sort() print(list_six ) #[1, 2, 3, 4, 5, 7, 8] list_seven= [1,3,5,2,4,7,8] list_seven .sort(reverse= True) print(list_seven ) # [8, 7, 5, 4, 3, 2, 1] list_eight = ["Zhangsan","lisi","wangwu"] list_eight .sort() print(list_eight ) # ['Zhangsan', 'lisi', 'wangwu'] 区分大小写 大写在前 list_eight = ["Zhangsan","lisi","wangwu"] list_eight .sort(key=str.lower) print(list_eight ) # ['lisi', 'wangwu', 'Zhangsan'] 不区分大小写 # sortde() sortde(list_name,key=None,reverse=False) list_nine = ["xiaoli","xiaohong","daming","Zhangzheng"] list_ten = sorted(list_nine ) print(list_ten ) #['Zhangzheng', 'daming', 'xiaohong', 'xiaoli']
e = ["小明","小红"] f = ["小张","小强"] print(e+f) #['小明', '小红', '小张', '小强']
H = ["乔布斯"] print(H*5) #['乔布斯', '乔布斯', '乔布斯', '乔布斯', '乔布斯']
num = [11,66,33,87,26,97,13,23,6,3,9] print("最大值为:",max(num)) # 最大值为:97 print("最小值为:",min(num)) # 最小值为:3 print("长度为:",len(num)) # 11

浙公网安备 33010602011771号