list() - 列表构造函数
创建列表对象或将可迭代对象转换为列表,包含完整的列表方法和操作指南
list() - 列表构造函数与完整列表操作指南
📝 概述
list() 是 Python 中的内置函数,用于创建列表对象或将可迭代对象转换为列表。列表是 Python 中最常用的数据结构之一,具有有序、可变、允许重复元素的特性,广泛应用于数据存储和处理。本文档涵盖列表的创建、方法使用、操作技巧和实际应用。
🎯 学习目标
- 掌握 list()函数的基本用法
- 理解列表的特性和应用场景
- 学会不同类型到列表的转换
- 掌握列表的所有内置方法和操作
- 了解列表的性能特点和最佳实践
- 学会列表与字符串的相互转换
- 掌握列表排序、复制和合并技巧
📋 前置知识
- Python 基本数据类型
- 可迭代对象概念
- 序列类型基础
- 索引和切片操作
🔍 详细内容
基本概念
列表(list)是 Python 中的可变序列类型,可以存储任意类型的对象。list() 函数可以创建空列表或将其他可迭代对象转换为列表,是数据处理中的基础工具。
语法格式
## 创建空列表
list()
## 从可迭代对象创建列表
list(iterable)
参数说明
| 参数名 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| iterable | iterable | 否 | 无 | 可迭代对象(字符串、元组、集合等) |
返回值
- 类型: list
- 说明: 新创建的列表对象
💡 实际应用
基础用法
## 创建空列表
empty_list = list()
print(f"空列表: {empty_list}") # 输出: 空列表: []
print(f"类型: {type(empty_list)}") # 输出: 类型: <class 'list'>
## 等价的创建方式
empty_list2 = []
print(f"字面量创建: {empty_list2}") # 输出: 字面量创建: []
## 从字符串创建列表
string_to_list = list("hello")
print(f"字符串转列表: {string_to_list}") # 输出: 字符串转列表: ['h', 'e', 'l', 'l', 'o']
## 从元组创建列表
tuple_data = (1, 2, 3, 4, 5)
tuple_to_list = list(tuple_data)
print(f"元组转列表: {tuple_to_list}") # 输出: 元组转列表: [1, 2, 3, 4, 5]
## 从集合创建列表
set_data = {3, 1, 4, 1, 5, 9, 2, 6}
set_to_list = list(set_data)
print(f"集合转列表: {set_to_list}") # 输出: 集合转列表: [1, 2, 3, 4, 5, 6, 9] (顺序可能不同)
## 从 range 对象创建列表
range_to_list = list(range(5))
print(f"range 转列表: {range_to_list}") # 输出: range 转列表: [0, 1, 2, 3, 4]
## 从字典创建列表(默认是键)
dict_data = {'a': 1, 'b': 2, 'c': 3}
dict_keys_to_list = list(dict_data)
print(f"字典键转列表: {dict_keys_to_list}") # 输出: 字典键转列表: ['a', 'b', 'c']
## 字典值转列表
dict_values_to_list = list(dict_data.values())
print(f"字典值转列表: {dict_values_to_list}") # 输出: 字典值转列表: [1, 2, 3]
## 字典项转列表
dict_items_to_list = list(dict_data.items())
print(f"字典项转列表: {dict_items_to_list}") # 输出: 字典项转列表: [('a', 1), ('b', 2), ('c', 3)]
生成器和迭代器转换
## 生成器表达式转列表
squares_gen = (x**2 for x in range(5))
squares_list = list(squares_gen)
print(f"平方数列表: {squares_list}") # 输出: 平方数列表: [0, 1, 4, 9, 16]
## 过滤器转列表
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_filter = filter(lambda x: x % 2 == 0, numbers)
even_list = list(even_filter)
print(f"偶数列表: {even_list}") # 输出: 偶数列表: [2, 4, 6, 8, 10]
## 映射器转列表
map_result = map(lambda x: x * 2, range(5))
map_list = list(map_result)
print(f"映射结果: {map_list}") # 输出: 映射结果: [0, 2, 4, 6, 8]
## 枚举器转列表
words = ['apple', 'banana', 'cherry']
enum_result = enumerate(words)
enum_list = list(enum_result)
print(f"枚举结果: {enum_list}") # 输出: 枚举结果: [(0, 'apple'), (1, 'banana'), (2, 'cherry')]
## zip 对象转列表
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
zip_result = zip(names, ages)
zip_list = list(zip_result)
print(f"zip 结果: {zip_list}") # 输出: zip 结果: [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
## 自定义迭代器转列表
class CountDown:
def __init__(self, start):
self.start = start
def __iter__(self):
return self
def __next__(self):
if self.start <= 0:
raise StopIteration
self.start -= 1
return self.start + 1
countdown = CountDown(5)
countdown_list = list(countdown)
print(f"倒计时列表: {countdown_list}") # 输出: 倒计时列表: [5, 4, 3, 2, 1]
文件和数据处理
## 读取文件行到列表
def read_file_to_list(filename):
"""读取文件内容到列表"""
try:
with open(filename, 'r', encoding='utf-8') as file:
lines = list(file) # 每行作为一个元素
# 去除换行符
clean_lines = [line.strip() for line in lines]
return clean_lines
except FileNotFoundError:
print(f"文件 {filename} 不存在")
return []
## 模拟文件内容
sample_content = """第一行内容
第二行内容
第三行内容
第四行内容"""
## 创建临时文件进行测试
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as temp_file:
temp_file.write(sample_content)
temp_filename = temp_file.name
## 读取文件到列表
file_lines = read_file_to_list(temp_filename)
print(f"文件行列表: {file_lines}")
## 清理临时文件
os.unlink(temp_filename)
## CSV 数据处理
csv_data = "name,age,city\nAlice,25,New York\nBob,30,London\nCharlie,35,Tokyo"
csv_lines = csv_data.split('\n')
csv_list = list(csv_lines)
print(f"CSV 行列表: {csv_list}")
## 解析 CSV 数据
header = csv_list[0].split(',')
data_rows = [row.split(',') for row in csv_list[1:]]
print(f"CSV 头部: {header}")
print(f"CSV 数据: {data_rows}")
## 转换为字典列表
csv_dicts = []
for row in data_rows:
row_dict = dict(zip(header, row))
csv_dicts.append(row_dict)
print(f"CSV 字典列表: {csv_dicts}")
数据结构转换和操作
## 嵌套结构展平
def flatten_list(nested_list):
"""展平嵌套列表"""
result = []
for item in nested_list:
if isinstance(item, list):
result.extend(flatten_list(item)) # 递归处理
else:
result.append(item)
return result
## 测试嵌套列表
nested = [1, [2, 3], [4, [5, 6]], 7, [8, [9, [10]]]]
flattened = flatten_list(nested)
print(f"原始嵌套: {nested}")
print(f"展平结果: {flattened}")
## 使用 itertools.chain 展平(更高效)
from itertools import chain
simple_nested = [[1, 2], [3, 4], [5, 6]]
chain_flattened = list(chain.from_iterable(simple_nested))
print(f"chain 展平: {chain_flattened}")
## 矩阵转置
def transpose_matrix(matrix):
"""矩阵转置"""
if not matrix or not matrix[0]:
return []
return list(zip(*matrix))
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = transpose_matrix(matrix)
print(f"原矩阵: {matrix}")
print(f"转置矩阵: {list(map(list, transposed))}")
## 列表去重(保持顺序)
def remove_duplicates_ordered(lst):
"""去重并保持顺序"""
seen = set()
result = []
for item in lst:
if item not in seen:
seen.add(item)
result.append(item)
return result
with_duplicates = [1, 2, 3, 2, 4, 1, 5, 3, 6]
unique_ordered = remove_duplicates_ordered(with_duplicates)
print(f"原列表: {with_duplicates}")
print(f"去重后: {unique_ordered}")
## 使用 dict.fromkeys 去重(Python 3.7+保持顺序)
unique_dict_method = list(dict.fromkeys(with_duplicates))
print(f"dict 方法去重: {unique_dict_method}")
## 列表分组
def group_by_key(lst, key_func):
"""按键函数分组"""
groups = {}
for item in lst:
key = key_func(item)
if key not in groups:
groups[key] = []
groups[key].append(item)
return groups
students = [
{'name': 'Alice', 'grade': 'A', 'age': 20},
{'name': 'Bob', 'grade': 'B', 'age': 19},
{'name': 'Charlie', 'grade': 'A', 'age': 21},
{'name': 'Diana', 'grade': 'B', 'age': 20}
]
## 按成绩分组
by_grade = group_by_key(students, lambda s: s['grade'])
print("按成绩分组:")
for grade, student_list in by_grade.items():
names = [s['name'] for s in student_list]
print(f" {grade}级: {names}")
## 按年龄分组
by_age = group_by_key(students, lambda s: s['age'])
print("\n 按年龄分组:")
for age, student_list in by_age.items():
names = [s['name'] for s in student_list]
print(f" {age}岁: {names}")
实际案例:数据分析
def analyze_sales_data(sales_records):
"""分析销售数据"""
if not sales_records:
return {}
# # 提取各种数据列表
products = list(set(record['product'] for record in sales_records))
sales_amounts = [record['amount'] for record in sales_records]
sales_dates = [record['date'] for record in sales_records]
# # 按产品分组
product_sales = {}
for record in sales_records:
product = record['product']
if product not in product_sales:
product_sales[product] = []
product_sales[product].append(record['amount'])
# # 计算统计信息
product_stats = {}
for product, amounts in product_sales.items():
product_stats[product] = {
'total': sum(amounts),
'average': sum(amounts) / len(amounts),
'count': len(amounts),
'max': max(amounts),
'min': min(amounts)
}
# # 按月份统计
monthly_sales = {}
for record in sales_records:
month = record['date'][:7] # YYYY-MM 格式
if month not in monthly_sales:
monthly_sales[month] = 0
monthly_sales[month] += record['amount']
# # 排序结果
top_products = sorted(product_stats.items(),
key=lambda x: x[1]['total'],
reverse=True)
return {
'products': products,
'total_sales': sum(sales_amounts),
'total_transactions': len(sales_records),
'average_transaction': sum(sales_amounts) / len(sales_records),
'product_stats': product_stats,
'monthly_sales': monthly_sales,
'top_products': top_products
}
## 测试销售数据
sales_data = [
{'date': '2024-01-15', 'product': 'iPhone', 'amount': 999, 'customer': 'Alice'},
{'date': '2024-01-16', 'product': 'MacBook', 'amount': 1999, 'customer': 'Bob'},
{'date': '2024-01-17', 'product': 'iPhone', 'amount': 999, 'customer': 'Charlie'},
{'date': '2024-01-18', 'product': 'iPad', 'amount': 599, 'customer': 'Diana'},
{'date': '2024-02-01', 'product': 'MacBook', 'amount': 1999, 'customer': 'Eve'},
{'date': '2024-02-02', 'product': 'iPhone', 'amount': 999, 'customer': 'Frank'},
{'date': '2024-02-03', 'product': 'iPad', 'amount': 599, 'customer': 'Grace'}
]
analysis = analyze_sales_data(sales_data)
print("销售数据分析:")
print(f"产品列表: {analysis['products']}")
print(f"总销售额: ${analysis['total_sales']:,}")
print(f"总交易数: {analysis['total_transactions']}")
print(f"平均交易额: ${analysis['average_transaction']:.2f}")
print("\n 产品统计:")
for product, stats in analysis['product_stats'].items():
print(f" {product}:")
print(f" 总销售: ${stats['total']:,}")
print(f" 平均价格: ${stats['average']:.2f}")
print(f" 销售次数: {stats['count']}")
print("\n 月度销售:")
for month, amount in analysis['monthly_sales'].items():
print(f" {month}: ${amount:,}")
print("\n 销售排行:")
for i, (product, stats) in enumerate(analysis['top_products'], 1):
print(f" {i}. {product}: ${stats['total']:,}")
高级列表操作
## 列表推导式与 list()结合
numbers = range(10)
## 条件过滤
even_squares = list(x**2 for x in numbers if x % 2 == 0)
print(f"偶数平方: {even_squares}")
## 嵌套推导
matrix = [[i*j for j in range(1, 4)] for i in range(1, 4)]
print(f"乘法表矩阵: {matrix}")
## 展平嵌套推导
flattened_matrix = list(item for row in matrix for item in row)
print(f"展平矩阵: {flattened_matrix}")
## 条件表达式
processed = list('positive' if x > 0 else 'negative' if x < 0 else 'zero'
for x in [-2, -1, 0, 1, 2])
print(f"数值分类: {processed}")
## 多重迭代
combinations = list((x, y) for x in [1, 2, 3] for y in ['a', 'b'] if x > 1)
print(f"条件组合: {combinations}")
## 函数式编程结合
from functools import reduce
from operator import add
## 累积操作
numbers = [1, 2, 3, 4, 5]
cumulative = []
for i in range(1, len(numbers) + 1):
cumulative.append(reduce(add, numbers[:i]))
print(f"累积和: {cumulative}")
## 更简洁的累积和
import itertools
cumsum = list(itertools.accumulate(numbers))
print(f"itertools 累积和: {cumsum}")
## 滑动窗口
def sliding_window(lst, window_size):
"""滑动窗口"""
return [lst[i:i+window_size] for i in range(len(lst)-window_size+1)]
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
windows = sliding_window(data, 3)
print(f"滑动窗口(大小 3): {windows}")
## 分块处理
def chunk_list(lst, chunk_size):
"""将列表分块"""
return [lst[i:i+chunk_size] for i in range(0, len(lst), chunk_size)]
chunks = chunk_list(data, 3)
print(f"分块处理(大小 3): {chunks}")
⚠️ 注意事项
性能考虑
import time
import sys
## 创建方式性能比较
n = 100000
## 方法 1: list()构造
start_time = time.time()
list1 = list(range(n))
time1 = time.time() - start_time
## 方法 2: 列表推导式
start_time = time.time()
list2 = [i for i in range(n)]
time2 = time.time() - start_time
## 方法 3: 预分配+循环
start_time = time.time()
list3 = [None] * n
for i in range(n):
list3[i] = i
time3 = time.time() - start_time
print(f"性能比较({n}个元素):")
print(f"list(range()): {time1:.6f}秒")
print(f"列表推导式: {time2:.6f}秒")
print(f"预分配+循环: {time3:.6f}秒")
## 内存使用比较
print(f"\n 内存使用:")
print(f"list 对象大小: {sys.getsizeof(list1)} 字节")
print(f"range 对象大小: {sys.getsizeof(range(n))} 字节")
print(f"生成器大小: {sys.getsizeof(x for x in range(n))} 字节")
浅拷贝与深拷贝
import copy
## 原始数据
original = [[1, 2], [3, 4], [5, 6]]
## 直接赋值(引用)
reference = original
print(f"原始数据: {original}")
print(f"引用: {reference}")
print(f"是否同一对象: {original is reference}")
## list()创建浅拷贝
shallow_copy = list(original)
print(f"浅拷贝: {shallow_copy}")
print(f"是否同一对象: {original is shallow_copy}")
print(f"内部列表是否同一对象: {original[0] is shallow_copy[0]}")
## 修改测试
original[0][0] = 999
print(f"\n 修改原始数据后:")
print(f"原始数据: {original}")
print(f"引用: {reference}")
print(f"浅拷贝: {shallow_copy}")
## 深拷贝
original2 = [[1, 2], [3, 4], [5, 6]]
deep_copy = copy.deepcopy(original2)
original2[0][0] = 888
print(f"\n 深拷贝测试:")
print(f"原始数据: {original2}")
print(f"深拷贝: {deep_copy}")
## 使用列表推导式创建深拷贝
original3 = [[1, 2], [3, 4], [5, 6]]
deep_copy_manual = [row[:] for row in original3] # 对于二维列表
original3[0][0] = 777
print(f"\n 手动深拷贝:")
print(f"原始数据: {original3}")
print(f"手动深拷贝: {deep_copy_manual}")
类型转换注意事项
## 字符串转换
string_data = "hello"
char_list = list(string_data)
print(f"字符串转列表: {char_list}")
## 数字字符串
number_string = "12345"
digit_list = list(number_string)
print(f"数字字符串转列表: {digit_list}")
## 如果想要数字列表
number_list = [int(digit) for digit in number_string]
print(f"数字列表: {number_list}")
## 字节串转换
byte_data = b"hello"
byte_list = list(byte_data)
print(f"字节串转列表: {byte_list}") # 输出 ASCII 码值
## 字节串转字符列表
char_from_bytes = list(byte_data.decode('utf-8'))
print(f"字节串转字符列表: {char_from_bytes}")
## None 值处理
try:
none_list = list(None)
except TypeError as e:
print(f"None 转换错误: {e}")
## 处理可能为 None 的情况
def safe_list_conversion(obj):
"""安全的列表转换"""
if obj is None:
return []
try:
return list(obj)
except TypeError:
return [obj] # 如果不可迭代,包装成单元素列表
test_values = [None, "hello", [1, 2, 3], 42, (4, 5, 6)]
for value in test_values:
result = safe_list_conversion(value)
print(f"{value} -> {result}")
大数据处理
## 处理大型数据集时的内存优化
def process_large_dataset(data_source, batch_size=1000):
"""批量处理大型数据集"""
batch = []
results = []
for item in data_source:
batch.append(item)
if len(batch) >= batch_size:
# # 处理批次
processed_batch = [item * 2 for item in batch] # 示例处理
results.extend(processed_batch)
batch = [] # 清空批次
# # 处理剩余数据
if batch:
processed_batch = [item * 2 for item in batch]
results.extend(processed_batch)
return results
## 模拟大型数据源
def large_data_generator(size):
"""生成大型数据的生成器"""
for i in range(size):
yield i
## 测试批量处理
large_data = large_data_generator(10000)
processed_results = process_large_dataset(large_data, batch_size=500)
print(f"处理了 {len(processed_results)} 个数据项")
print(f"前 10 个结果: {processed_results[:10]}")
print(f"后 10 个结果: {processed_results[-10:]}")
## 内存友好的数据转换
def memory_efficient_conversion(iterable, chunk_size=1000):
"""内存友好的大型可迭代对象转换"""
result = []
chunk = []
for item in iterable:
chunk.append(item)
if len(chunk) >= chunk_size:
result.extend(chunk)
chunk = [] # 释放内存
if chunk:
result.extend(chunk)
return result
## 比较内存使用
import tracemalloc
## 开始内存跟踪
tracemalloc.start()
## 直接转换大型 range
large_range = range(100000)
direct_list = list(large_range)
current, peak = tracemalloc.get_traced_memory()
print(f"直接转换内存使用: 当前 {current / 1024 / 1024:.2f} MB, 峰值 {peak / 1024 / 1024:.2f} MB")
tracemalloc.stop()
🏷️ 列表方法入门合集
用下标访问列表元素
列表的下标操作
## 下标访问
name = ['alex', 'tell', 'Dsion', 'ming']
print(name) # 输出完整列表
print(name[0]) # 输出: alex (第一个元素)
a = name[1] # 将元素赋值给变量
print(a) # 输出: tell
print(name[-1]) # 输出: ming (最后一个元素)
## 下标对应关系
print(f"name[0] = {name[0]}") # 正向索引
print(f"name[-4] = {name[-4]}") # 负向索引,等价于 name[0]
print(f"name[1] = {name[1]}") # 正向索引
index 方法查找下标
name = ['alex', 'tell', 'Dsion', 'ming']
## 查找元素的索引位置
print(name.index('Dsion')) # 输出: 2
print(name.index('ming')) # 输出: 3
## 处理不存在的元素
try:
print(name.index('notfound'))
except ValueError as e:
print(f"元素不存在: {e}")
切片操作详解
name = ['alex', 'tell', 'Dsion', 'ming']
## 各种切片方式
print(name[:]) # 输出: ['alex', 'tell', 'Dsion', 'ming'] (所有元素)
print(name[1:]) # 输出: ['tell', 'Dsion', 'ming'] (从索引1到末尾)
print(name[:3]) # 输出: ['alex', 'tell', 'Dsion'] (从开始到索引3之前)
print(name[:-1]) # 输出: ['alex', 'tell', 'Dsion'] (除了最后一个元素)
print(name[1:3]) # 输出: ['tell', 'Dsion'] (从索引1到索引3之前)
print(name[-3:-1]) # 输出: ['tell', 'Dsion'] (负索引切片)
## 步长切片
print(name[::2]) # 输出: ['alex', 'Dsion'] (每隔一个元素)
print(name[::-1]) # 输出: ['ming', 'Dsion', 'tell', 'alex'] (反向)
对列表进行添加元素
append() 方法 - 末尾添加
name = ['alex', 'tell', 'Dsion', 'ming']
name.append('Xxitf')
print(name) # 输出: ['alex', 'tell', 'Dsion', 'ming', 'Xxitf']
## append 添加的是整个对象
numbers = [1, 2, 3]
numbers.append([4, 5])
print(numbers) # 输出: [1, 2, 3, [4, 5]] (注意是嵌套列表)
insert() 方法 - 任意位置添加
name = ['alex', 'tell', 'Dsion', 'ming']
name.insert(2, 'Xxitf') # 在索引2位置插入
print(name) # 输出: ['alex', 'tell', 'Xxitf', 'Dsion', 'ming']
## 在开头插入
name.insert(0, 'First')
print(name) # 输出: ['First', 'alex', 'tell', 'Xxitf', 'Dsion', 'ming']
## 在末尾插入(等价于append)
name.insert(len(name), 'Last')
print(name)
extend() 方法 - 扩展列表
list1 = [1, 2, 3]
list2 = [4, 5, 6]
## extend 与 append 的区别
list1.extend(list2)
print(list1) # 输出: [1, 2, 3, 4, 5, 6]
list3 = [1, 2, 3]
list3.append(list2)
print(list3) # 输出: [1, 2, 3, [4, 5, 6]]
## extend 可以接受任何可迭代对象
letters = ['a', 'b']
letters.extend('cd')
print(letters) # 输出: ['a', 'b', 'c', 'd']
删除列表元素
del 语句 - 永久删除
name = ['alex', 'tell', 'Dsion', 'ming']
## 删除单个元素
del name[0]
print(name) # 输出: ['tell', 'Dsion', 'ming']
## 删除切片
name = ['alex', 'tell', 'Dsion', 'ming']
del name[0:2]
print(name) # 输出: ['Dsion', 'ming']
## 删除整个列表
test_list = [1, 2, 3]
del test_list
# print(test_list) # 这会报错,因为列表已被删除
pop() 方法 - 删除并返回元素
name = ['alex', 'tell', 'Dsion', 'ming']
## 删除末尾元素
last_item = name.pop()
print(f"删除的元素: {last_item}") # 输出: 删除的元素: ming
print(name) # 输出: ['alex', 'tell', 'Dsion']
## 删除指定位置元素
name = ['alex', 'tell', 'Dsion', 'ming']
removed_item = name.pop(1)
print(f"删除的元素: {removed_item}") # 输出: 删除的元素: tell
print(name) # 输出: ['alex', 'Dsion', 'ming']
## 空列表 pop 会报错
empty_list = []
try:
empty_list.pop()
except IndexError as e:
print(f"错误: {e}")
remove() 方法 - 按值删除
name = ['alex', 'tell', 'Dsion', 'ming']
## 删除第一个匹配的元素
name.remove('Dsion')
print(name) # 输出: ['alex', 'tell', 'ming']
## 删除不存在的元素会报错
try:
name.remove('notfound')
except ValueError as e:
print(f"错误: {e}")
## 列表中有重复元素时
numbers = [1, 2, 3, 2, 4, 2]
numbers.remove(2) # 只删除第一个2
print(numbers) # 输出: [1, 3, 2, 4, 2]
clear() 方法 - 清空列表
name = ['alex', 'tell', 'Dsion', 'ming']
name.clear()
print(name) # 输出: []
字符串与列表间的转换
join() 方法 - 列表转字符串
## 基本用法
a = ['yes', 'no', 'yes', 'no', 'yes', 'no']
b = 'or'.join(a)
print(b) # 输出: yesornooryesornooryesorno
## 不同分隔符
words = ['apple', 'banana', 'cherry']
print(' '.join(words)) # 输出: apple banana cherry
print(', '.join(words)) # 输出: apple, banana, cherry
print('-'.join(words)) # 输出: apple-banana-cherry
print(''.join(words)) # 输出: applebananacherry
## 数字列表需要先转换为字符串
numbers = [1, 2, 3, 4, 5]
number_strings = [str(num) for num in numbers]
result = ','.join(number_strings)
print(result) # 输出: 1,2,3,4,5
## 更简洁的写法
result2 = ','.join(map(str, numbers))
print(result2) # 输出: 1,2,3,4,5
split() 方法 - 字符串转列表
## 基本用法
b = 'yesornooryesornooryesorno'
result = b.split('or')
print(result) # 输出: ['yes', 'no', 'yes', 'no', 'yes', 'no']
## 不同分隔符
text = 'apple,banana,cherry'
fruits = text.split(',')
print(fruits) # 输出: ['apple', 'banana', 'cherry']
## 按空格分割
sentence = 'Hello world Python programming'
words = sentence.split()
print(words) # 输出: ['Hello', 'world', 'Python', 'programming']
## 限制分割次数
text = 'a-b-c-d-e'
parts = text.split('-', 2) # 最多分割2次
print(parts) # 输出: ['a', 'b', 'c-d-e']
## 按行分割
multiline = "第一行\n第二行\n第三行"
lines = multiline.split('\n')
print(lines) # 输出: ['第一行', '第二行', '第三行']
🔄 列表变换常见函数使用
对列表进行排序
sort() 方法 - 原地排序
## 字母排序
name = ['alex', 'tell', 'Dsion', 'ming']
name.sort()
print(name) # 输出: ['Dsion', 'alex', 'ming', 'tell'] (按ASCII码排序)
## 数字排序
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
numbers.sort()
print(numbers) # 输出: [1, 1, 2, 3, 4, 5, 6, 9]
## 反向排序
name = ['alex', 'tell', 'Dsion', 'ming']
name.sort(reverse=True)
print(name) # 输出: ['tell', 'ming', 'alex', 'Dsion']
## 忽略大小写排序
words = ['apple', 'Banana', 'cherry', 'Date']
words.sort(key=str.lower)
print(words) # 输出: ['apple', 'Banana', 'cherry', 'Date']
## 按长度排序
words = ['apple', 'fig', 'banana', 'kiwi']
words.sort(key=len)
print(words) # 输出: ['fig', 'kiwi', 'apple', 'banana']
sorted() 函数 - 临时排序
name = ['alex', 'tell', 'Dsion', 'ming']
## 临时排序,不改变原列表
sorted_name = sorted(name)
print(f"排序后: {sorted_name}") # 输出: ['Dsion', 'alex', 'ming', 'tell']
print(f"原列表: {name}") # 输出: ['alex', 'tell', 'Dsion', 'ming']
## 反向临时排序
reverse_sorted = sorted(name, reverse=True)
print(f"反向排序: {reverse_sorted}")
## 复杂对象排序
students = [
{'name': 'Alice', 'grade': 88},
{'name': 'Bob', 'grade': 92},
{'name': 'Charlie', 'grade': 85}
]
## 按成绩排序
by_grade = sorted(students, key=lambda x: x['grade'])
print("按成绩排序:")
for student in by_grade:
print(f" {student['name']}: {student['grade']}")
使用 lambda 进行自定义排序
## Lambda 函数示例
numbers = [1, 2, 3, 4, 5]
## 按负值排序(等同于 reverse=True)
numbers.sort(key=lambda x: x * -1)
print(numbers) # 输出: [5, 4, 3, 2, 1]
## 按绝对值排序
numbers = [-3, -1, 0, 1, 3, -2, 2]
numbers.sort(key=lambda x: abs(x))
print(numbers) # 输出: [0, -1, 1, -2, 2, -3, 3]
## 字符串按长度再按字母顺序排序
words = ['apple', 'fig', 'banana', 'kiwi', 'grape']
words.sort(key=lambda x: (len(x), x))
print(words) # 输出: ['fig', 'kiwi', 'apple', 'grape', 'banana']
对于列表相关的计算
len() 函数 - 计算列表长度
name = ['alex', 'tell', 'Dsion', 'ming']
print(len(name)) # 输出: 4
## 空列表长度
empty = []
print(len(empty)) # 输出: 0
## 嵌套列表长度(只计算顶层元素)
nested = [[1, 2], [3, 4, 5], [6]]
print(len(nested)) # 输出: 3
min() 和 max() - 取最大和最小值
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(f"最小值: {min(numbers)}") # 输出: 最小值: 1
print(f"最大值: {max(numbers)}") # 输出: 最大值: 9
## 字符串列表
words = ['apple', 'banana', 'cherry']
print(f"最小值: {min(words)}") # 输出: 最小值: apple (按字典序)
print(f"最大值: {max(words)}") # 输出: 最大值: cherry
## 按长度比较
print(f"最短: {min(words, key=len)}") # 输出: 最短: apple
print(f"最长: {max(words, key=len)}") # 输出: 最长: banana
## 空列表会报错
try:
min([])
except ValueError as e:
print(f"错误: {e}")
sum() 函数用法
numbers = [1, 3, 2, 6, 8, 4]
print(sum(numbers)) # 输出: 24
## 带初始值的求和
print(sum(numbers, 10)) # 输出: 34 (24 + 10)
## 浮点数求和
float_numbers = [1.1, 2.2, 3.3]
print(sum(float_numbers)) # 输出: 6.6
## 字符串列表无法直接求和
strings = ['1', '2', '3']
try:
sum(strings)
except TypeError as e:
print(f"错误: {e}")
## 需要先转换
string_numbers = [int(x) for x in strings]
print(sum(string_numbers)) # 输出: 6
## 使用 numpy 的优势(需要安装 numpy)
try:
import numpy as np
large_list = list(range(1000000))
# Python 内置 sum
import time
start = time.time()
result1 = sum(large_list)
python_time = time.time() - start
# NumPy sum
np_array = np.array(large_list)
start = time.time()
result2 = np.sum(np_array)
numpy_time = time.time() - start
print(f"Python sum 用时: {python_time:.4f}秒")
print(f"NumPy sum 用时: {numpy_time:.4f}秒")
print(f"NumPy 快了 {python_time/numpy_time:.2f} 倍")
except ImportError:
print("NumPy 未安装,跳过性能比较")
列表合并
列表相加
a = ['hello', 'world']
b = ['python', 'programming']
result = a + b
print(result) # 输出: ['hello', 'world', 'python', 'programming']
## 原列表不变
print(f"a: {a}") # 输出: a: ['hello', 'world']
print(f"b: {b}") # 输出: b: ['python', 'programming']
## 多个列表相加
c = ['!']
combined = a + b + c
print(combined) # 输出: ['hello', 'world', 'python', 'programming', '!']
列表乘法复制
b = ['yes', 'no']
repeated = b * 3
print(repeated) # 输出: ['yes', 'no', 'yes', 'no', 'yes', 'no']
## 创建初始化列表
zeros = [0] * 5
print(zeros) # 输出: [0, 0, 0, 0, 0]
## 注意:引用问题
matrix_wrong = [[0] * 3] * 3 # 错误方式
matrix_wrong[0][0] = 1
print(matrix_wrong) # 输出: [[1, 0, 0], [1, 0, 0], [1, 0, 0]] (所有行都被修改)
## 正确方式
matrix_right = [[0] * 3 for _ in range(3)]
matrix_right[0][0] = 1
print(matrix_right) # 输出: [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
列表赋值
使用下标赋值
a = ['yes', 'no', 'yes', 'no', 'yes', 'no']
a[2] = 'xxx'
print(a) # 输出: ['yes', 'no', 'xxx', 'no', 'yes', 'no']
## 批量赋值
a[1:3] = ['new1', 'new2']
print(a) # 输出: ['yes', 'new1', 'new2', 'no', 'yes', 'no']
## 不等长替换
a[0:2] = ['replaced']
print(a) # 输出: ['replaced', 'new2', 'no', 'yes', 'no']
整体赋值(多重赋值)
b = ['yes', 'no']
b[:] = ['abb', 'bba'] # 必须元素个数相等
print(b) # 输出: ['abb', 'bba']
## 不等长赋值
b[:] = ['one', 'two', 'three']
print(b) # 输出: ['one', 'two', 'three']
## 使用可迭代对象赋值
b[:] = 'hello'
print(b) # 输出: ['h', 'e', 'l', 'l', 'o']
list() 和 tuple() 函数
list() 函数转换
## 字符串转列表
c = 'lallaaa'
char_list = list(c)
print(char_list) # 输出: ['l', 'a', 'l', 'l', 'a', 'a', 'a']
print(c) # 输出: lallaaa (原字符串不变)
## 元组转列表
tuple_data = (1, 2, 3, 4)
list_data = list(tuple_data)
print(list_data) # 输出: [1, 2, 3, 4]
## 集合转列表
set_data = {3, 1, 4, 1, 5}
list_from_set = list(set_data)
print(list_from_set) # 输出: [1, 3, 4, 5] (顺序可能不同)
tuple() 函数转换
b = ['yes', 'no']
tuple_data = tuple(b)
print(tuple_data) # 输出: ('yes', 'no')
print(b) # 输出: ['yes', 'no'] (原列表不变)
## 字符串转元组
string_tuple = tuple('hello')
print(string_tuple) # 输出: ('h', 'e', 'l', 'l', 'o')
复制列表的方法
切片复制
a = ['yes', 'no', 'yes', 'no', 'yes', 'no']
b = a[:] # 浅拷贝
b[0] = 'changed'
print(f"a: {a}") # 输出: a: ['yes', 'no', 'yes', 'no', 'yes', 'no']
print(f"b: {b}") # 输出: b: ['changed', 'no', 'yes', 'no', 'yes', 'no']
## 等价的复制方法
c = a.copy()
d = list(a)
copy 模块复制列表
import copy
## 浅拷贝
a = ['yes', 'no', 'yes', 'no', 'yes', 'no']
b = copy.copy(a)
## 深拷贝(用于嵌套列表)
nested_list = [['a', 'b', 'c'], [1, 2, 3], ['ss', 'ff', 'tt']]
shallow_copy = copy.copy(nested_list)
deep_copy = copy.deepcopy(nested_list)
## 修改嵌套列表元素
nested_list[0][0] = 'modified'
print(f"原列表: {nested_list}")
print(f"浅拷贝: {shallow_copy}") # 受影响
print(f"深拷贝: {deep_copy}") # 不受影响
## 直接赋值的问题
original = [1, 2, 3]
direct_assignment = original # 这不是拷贝!
direct_assignment[0] = 'changed'
print(f"原列表: {original}") # 输出: 原列表: ['changed', 2, 3] (也被修改了)
嵌套列表的读取
nested = [['a', 'b', 'c'], [1, 2, 3], ['ss', 'ff', 'tt']]
## 访问嵌套元素
print(nested[2][0]) # 输出: ss
print(nested[0][1]) # 输出: b
print(nested[1][2]) # 输出: 3
## 修改嵌套元素
nested[0][0] = 'A'
print(nested) # 输出: [['A', 'b', 'c'], [1, 2, 3], ['ss', 'ff', 'tt']]
## 三维嵌套
three_d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(three_d[0][1][0]) # 输出: 3
## 安全访问嵌套元素
def safe_get(nested_list, *indices):
"""安全获取嵌套列表元素"""
try:
result = nested_list
for index in indices:
result = result[index]
return result
except (IndexError, TypeError):
return None
print(safe_get(nested, 2, 0)) # 输出: ss
print(safe_get(nested, 5, 0)) # 输出: None (索引超出范围)
🔍 高级列表操作
列表推导式
## 基本列表推导式
squares = [x**2 for x in range(10)]
print(squares) # 输出: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
## 带条件的列表推导式
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # 输出: [0, 4, 16, 36, 64]
## 嵌套列表推导式
matrix = [[i*j for j in range(3)] for i in range(3)]
print(matrix) # 输出: [[0, 0, 0], [0, 1, 2], [0, 2, 4]]
## 字符串处理
words = ['hello', 'world', 'python']
upper_words = [word.upper() for word in words]
print(upper_words) # 输出: ['HELLO', 'WORLD', 'PYTHON']
列表方法总结
## 所有列表方法演示
demo_list = [1, 2, 3]
## 添加方法
demo_list.append(4) # 末尾添加
demo_list.insert(0, 0) # 指定位置插入
demo_list.extend([5, 6]) # 扩展列表
print(f"添加后: {demo_list}")
## 删除方法
demo_list.remove(3) # 按值删除
popped = demo_list.pop() # 删除并返回末尾元素
del demo_list[0] # 按索引删除
print(f"删除后: {demo_list}")
print(f"弹出的元素: {popped}")
## 查找和统计
demo_list = [1, 2, 3, 2, 4, 2]
print(f"元素2的索引: {demo_list.index(2)}")
print(f"元素2的数量: {demo_list.count(2)}")
## 排序和反转
demo_list.sort()
print(f"排序后: {demo_list}")
demo_list.reverse()
print(f"反转后: {demo_list}")
## 复制
copied_list = demo_list.copy()
print(f"复制的列表: {copied_list}")
## 清空
demo_list.clear()
print(f"清空后: {demo_list}")
⚠️ 注意事项
- 列表是可变对象,修改操作会改变原列表
- 使用
list()进行类型转换时要注意性能,大数据量时考虑生成器 - 嵌套列表复制时要区分浅拷贝和深拷贝
- 索引越界会抛出
IndexError异常 remove()删除不存在的元素会抛出ValueError异常- 空列表调用
pop()会抛出IndexError异常 - 列表推导式虽然简洁,但复杂逻辑建议使用传统循环
- 大量字符串连接建议使用
join()而不是+操作符
🔗 相关内容
相关模块
相关概念
📚 扩展阅读
🏷️ 标签
list 列表 构造函数 类型转换 容器 序列 列表方法 列表操作 切片 索引
最后更新: 2024-12-19
作者: Python 文档工程师
版本: 2.0
讨论与反馈
欢迎在下方留言讨论,分享你的学习心得或提出问题。评论基于GitHub Issues,需要GitHub账号。