python学习(二十五) 链表方法

# 链表
cars = ['a', "b"]

print(cars)
# 链表长度
print(len(cars))

# 结尾添加元素
cars.append("c")
cars.append("d")
print(cars)

# 指定位置添加元素
cars.insert(2, "hello")
print(cars)

# 查找元素位置
print(cars.index('hello'))

# 链表末尾弹出元素
cars.pop()
print(cars)
cars.pop(2)
print(cars)

# 删除链表的元素
cars.remove("b")
print(cars)

# 排序
cars.sort()

result:

['a', 'b']
2
['a', 'b', 'c', 'd']
['a', 'b', 'hello', 'c', 'd']
2
['a', 'b', 'hello', 'c']
['a', 'b', 'c']
['a', 'c']