python列表的用法
python列表的用法
Python 中的列表(list)是一种非常常用的数据结构,用于存储有序的元素集合。列表是可变的(mutable),可以动态添加、删除或修改元素。以下是 Python 列表的常见用法和操作:
1. 创建列表
列表用方括号 [] 表示,元素之间用逗号分隔。
# 创建一个空列表
empty_list = []
# 创建包含元素的列表
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", 3.14, True]
2. 访问列表元素
通过索引访问列表中的元素,索引从 0 开始。
fruits = ["apple", "banana", "cherry"]
# 访问第一个元素
print(fruits[0]) # 输出: apple
# 访问最后一个元素
print(fruits[-1]) # 输出: cherry
# 访问子列表(切片)
print(fruits[1:3]) # 输出: ["banana", "cherry"]
3. 修改列表元素
列表是可变的,可以通过索引直接修改元素。
fruits = ["apple", "banana", "cherry"]
# 修改第二个元素
fruits[1] = "blueberry"
print(fruits) # 输出: ["apple", "blueberry", "cherry"]
4. 添加元素
append():在列表末尾添加一个元素。
insert():在指定位置插入一个元素。
extend():将另一个列表的所有元素添加到当前列表。
fruits = ["apple", "banana"]
# 在末尾添加元素
fruits.append("cherry")
print(fruits) # 输出: ["apple", "banana", "cherry"]
# 在指定位置插入元素
fruits.insert(1, "orange")
print(fruits) # 输出: ["apple", "orange", "banana", "cherry"]
# 合并另一个列表
more_fruits = ["mango", "grape"]
fruits.extend(more_fruits)
print(fruits) # 输出: ["apple", "orange", "banana", "cherry", "mango", "grape"]
5. 删除元素
remove():删除指定值的第一个匹配项。
pop():删除指定索引的元素(默认删除最后一个元素)。
del:删除指定索引的元素或整个列表。
clear():清空列表。
fruits = ["apple", "banana", "cherry", "banana"]
# 删除指定值的第一个匹配项
fruits.remove("banana")
print(fruits) # 输出: ["apple", "cherry", "banana"]
# 删除指定索引的元素
fruits.pop(1) # 删除索引为1的元素
print(fruits) # 输出: ["apple", "banana"]
# 删除整个列表或指定索引的元素
del fruits[0] # 删除索引为0的元素
print(fruits) # 输出: ["banana"]
# 清空列表
fruits.clear()
print(fruits) # 输出: []
6. 列表长度
使用 len() 函数获取列表的长度。
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # 输出: 3
7. 遍历列表
使用 for 循环遍历列表。
fruits = ["apple", "banana", "cherry"]
# 遍历列表
for fruit in fruits:
print(fruit)
# 带索引遍历
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
8. 列表排序
sort():对列表进行原地排序(修改原列表)。#有些面试题会问的列表排序 -_-
sorted():返回一个新的排序列表(不修改原列表)。
reverse():反转列表。
numbers = [3, 1, 4, 1, 5, 9, 2]
# 原地排序
numbers.sort()
print(numbers) # 输出: [1, 1, 2, 3, 4, 5, 9]
# 返回新排序列表
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers) # 输出: [9, 5, 4, 3, 2, 1, 1]
# 反转列表
numbers.reverse()
print(numbers) # 输出: [9, 5, 4, 3, 2, 1, 1]
9. 列表推导式
列表推导式是一种简洁的创建列表的方式。
# 创建一个包含1到10的平方的列表
squares = [x**2 for x in range(1, 11)]
print(squares) # 输出: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 带条件的列表推导式
even_squares = [x**2 for x in range(1, 11) if x % 2 == 0]
print(even_squares) # 输出: [4, 16, 36, 64, 100]
10. 列表复制
浅拷贝:使用切片或 copy() 方法。拷贝父对象,不会拷贝对象内部的子对象。
深拷贝:使用 copy.deepcopy()。拷贝父对象以及对象内部的所有子对象。
import copy
original = [1, 2, [3, 4]]
# 浅拷贝
shallow_copy = original.copy()
shallow_copy[2][0] = 99
print(original) # 输出: [1, 2, [99, 4]]
# 深拷贝
deep_copy = copy.deepcopy(original)
deep_copy[2][0] = 100
print(original) # 输出: [1, 2, [99, 4]]
11. 其他常用操作
index():返回指定值的第一个匹配项的索引。
count():返回指定值在列表中出现的次数。
in 关键字:检查元素是否在列表中。
fruits = ["apple", "banana", "cherry", "banana"]
# 查找索引
print(fruits.index("banana")) # 输出: 1
# 统计出现次数
print(fruits.count("banana")) # 输出: 2
# 检查元素是否存在
print("apple" in fruits) # 输出: True
12.列表去重–面试问的多
使用set()函数
lst = [1, 2, 3, 3, 4, 5, 5, 6]
new_lst = list(set(lst))
使用字典的fromkeys()方法
lst = [1, 2, 3, 3, 4, 5, 5, 6]
new_lst = list(dict.fromkeys(lst))
使用循环遍历
lst = [1, 2, 3, 3, 4, 5, 5, 6]
new_lst = []
for x in lst:
if x not in new_lst:
new_lst.append(x)