关于列表的基本操作
本len append pop extend remove insert:
- len计算列表长度
- append列表后添加一项
- pop除去最后一项
- extend从后面添加数据项集合
- remove删除列表中已存在的某项,不存在会报错
- insert(位置,项)在某一位置添加某项
代码块
(1)
cast = ["Cleese", "Palin", "Jones", "Idle"]
print(cast)
print(len(cast))
print(cast[1])
>>> ['Cleese', 'Palin', 'Jones', 'Idle']
>>> 4
>>> Palin
(2)
cast.append("Gilliam")
print(cast)
>>> ['Cleese', 'Palin', 'Jones', 'Idle', 'Gilliam']
(3)
cast.pop()
print(cast)
>>> ['Cleese', 'Palin', 'Jones', 'Idle']
(4)
cast.extend(["Gilliam", "Chapman"])
print(cast)
>>> ['Cleese', 'Palin', 'Jones', 'Idle', 'Gilliam', 'Chapman']
(5)
cast.remove("Chapman")
print(cast)
>>> ['Cleese', 'Palin', 'Jones', 'Idle', 'Gilliam']
(6)
cast.insert(0, "Chapman")
print(cast)
>>> ['Chapman', 'Cleese', 'Palin', 'Jones', 'Idle', 'Gilliam']