初始化l1 = [] # 空列表
l2 = [1, "a", 2]
print(l1, l2) # [] [1, 'a', 2]
l3 = list() # list()括号中放可迭代对象
l4 = list(range(3))
l5 = list("hello")
print(l3, l4, l5) # [] [0, 1, 2] ['h', 'e', 'l', 'l', 'o']插入lst = ['a', 'b', 'c']
lst.insert(1, '0') # 在原列表指定索引位置插入值
print(lst) # 结果为:['a', '0', 'b', 'c']追加lst = ['a', 'b']
lst.append('c') # 在原列表尾部追加
print(lst) # 结果为:['a', 'b', 'c']批量追加lst1 = ['a', 'b']
lst2 = ['c', 'd']
lst2.extend(lst1) # 将列表1中的元素添加到列表2
print(lst2) # 结果为:['c', 'd', 'a',
编程
· 2023-08-14
· 173 人浏览
三毛