list 增 append insert extend 运用
#list 增加 添加一个对象致list 末尾 # append k1 = [1,2,3,4,5,6,7,8,9,] k1.append("10") k1.append("python") k1.append([1,1,2,23,5,4]), print(k1) # [1, 2, 3, 4, 5, 6, 7, 8, 9, '10', 'python', [1, 1, 2, 23, 5, 4]]
# 插入 insert k1 = [1,2,3,4,5,6,7,8,9,] k1.insert(2,"nihao") # (需要插入的位置, “需要插入的内容”) print(k1) # [1, 2, 'nihao', 3, 4, 5, 6, 7, 8, 9]
# 用 for in 的方式 插入 实现列表的相融 l1 = [1,2,3,6] l3 = [7,8,9,5,4] for i in l3: l1.append(i) print(l1) # [1, 2, 3, 6, 7, 8, 9, 5, 4]
# extend: 用法 k1 = [1,2,3,4,5,6,7,8,9,] # extend: 把可迭代对象里的每一个元素添加至 list列表末尾 k1.extend("nihao") print(k1) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 'n', 'i', 'h', 'a', 'o'] k1.extend([59,89,65]) print(k1) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 59, 89, 65]