P3-列表简介

 1 bicycles = ['trek', 'cannondale', 'redline', 'specialized', 'remove']
 2 print(bicycles)
 3 print(bicycles[0])
 4 print(bicycles[0].title())
 5 print(bicycles[-1])
 6 print(bicycles[-2])
 7 bicycles[0] = '修改第一辆车'
 8 print(bicycles)
 9 # append增加到末尾
10 bicycles.append('增加一辆车')
11 print(bicycles)
12 # 插入需要指定下标
13 bicycles.insert(0, '插入一个')
14 print(bicycles)
15 # 删除插入的车
16 del bicycles[0]
17 print(bicycles)
18 # 弹出
19 pop = bicycles.pop()
20 print(pop)
21 pop2 = bicycles.pop(2)
22 print(pop2)
23 # 根据值删除元素 只删除第一次指定的值
24 bicycles.remove('remove')
25 print(bicycles)
 1 cars = ['bmw', 'audi', 'toyota', 'subaru']
 2 # 排序sort
 3 cars.sort()
 4 print(cars)
 5 cars.sort(reverse=True)
 6 print(cars)
 7 # 临时排序 sorted
 8 print(sorted(cars))
 9 print(cars)
10 # 反转 reverse()
11 cars.reverse()
12 print(cars)
13 # 获取长度
14 print(len(cars))

 

posted @ 2021-04-06 21:33  野兽Gentleman  阅读(40)  评论(0编辑  收藏  举报