1. f字符串的使用方法。
2. 创建列表:列表名[‘’,‘’,‘’,…]
3. 访问列表元素:列表名[索引值]
4. 列表的索引从0开始,倒数时从-1开始。
5. 使用列表中的各个值:列表名[索引值]
6. 修改、添加、和删除元素:
| 修改 | 添加 | 删除 |
|---|---|---|
| 列表名[索引] = ‘新元素’ | 末尾添加 :数组名.append(新元素’),这里要注意:append()方法不能直接在print语句中打印出来,要先添加元素,再打印被修改后的列表 | del语句:del 数组名[索引] |
| 插入:insert(索引值,‘新元素’) | 弹出方法pop():数组名.pop(括号内添加索引,默认为数组中最后一个数据) | |
| 根据值删除,只删除第一个指定的数值,如果后面还有,需要通过循环判断:方法remove(‘元素’) |
7. 组织列表:
| 永久性按字母顺序排序 | 临时性排序 | 倒着打印列表 | 确定列表长度 |
|---|---|---|---|
| 方法sort() | 函数sorted(列表名) | 方法reverse(),reverse()不是按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序 | 函数len(列表名) |
#3.1————列表是是什么
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
#3.1.1————访问列表元素
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())
print(bicycles[1].title())
print(bicycles[2].title())
print(bicycles[3].title())
#print(bicycles[4])
#3.1.2————索引从0开始而不是1开始
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
print(bicycles[-2])
print(bicycles[-3])
print(bicycles[-4])
#3.1.3————使用列表中的各个值
first_name = "ada"
last_name = "lovelace"
#f字符串:f"引号内为字符串和{变量名},无先后顺序,无运算符号"
full_name = f"{first_name}{last_name}"
print(full_name)
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicyle was a {bicycles[0].title()}."
print(message)
#3.2.1————修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'coolboy'
print(motorcycles)
#3.2.2————在列表中添加元素
#1.在列表末尾添加元素-append
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
motorcycles = []
print(motorcycles)
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
#2.在列表末尾添加元素-insert
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.insert(100, 'ducati')
print(motorcycles)
#3.2.3————在列表中删除元素
#1.使用del语句删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[2]
print(motorcycles)
#2.使用方法pop(索引,默认为0)删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
popped_motorcycle = motorcycles.pop(0)
print(f"The last motorcyle I owned was a {popped_motorcycle}.")
#3.根据值删除元素
motorcycles = ['ducati', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
#3.3————组织列表
#3.3.1.使用方法sort()对列表永久排序
#按字母排序
cars = ['BYD', 'BMW', 'AUDI', 'TOYOTA']
print(cars)
cars.sort()
print(cars)
#按字母顺序相反的顺序排序
cars.sort(reverse=True)
print(cars)
#3.3.2使用函数sorted()对列表临时排序
cars = ['BYD', 'BMW', 'AUDI', 'TOYOTA']
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print(cars)
#3.3.3倒着打印列表——reverse()永久修改
#reverse()不是按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序
cars = ['SUBARU', 'BMW', 'AUDI', 'TOYOTA']
print(cars)
cars.reverse()
print(cars)
print(cars)
cars.reverse()
print(cars)
#3.3.4确定列表的长度——len()
cars = ['SUBARU', 'BMW', 'AUDI', 'TOYOTA']
print(len(cars))
#3.4————使用列表时避免索引错误
motorcycles = ['ducati', 'yamaha', 'suzuki']
print(len(motorcycles))
print(motorcycles[2])
本文介绍了Python中列表的基本操作,包括创建、访问、修改、添加、删除列表元素的方法,以及如何使用sort()、sorted()和reverse()等函数来组织列表。同时,文章还展示了如何避免常见的索引错误。





