序列
列表
列表推导式体现了Python"优美优于丑陋"的哲学。它提供了一种简洁的方式来创建列表,让代码更加Pythonic。
# 传统方式
result = []
for x in range(10):
if x % 2 == 0:
result.append(x**2)
# Pythonic方式
result = [x**2 for x in range(10) if x % 2 == 0]
列表方法概览
list函数
list() 函数用于创建一个新的列表,或者将一个可迭代对象转换为列表。
list() 函数签名: list([iterable]) -> new list
参数说明:
iterable(可选): 一个可迭代对象,如字符串、元组、集合、字典等。如果省略,则创建一个空列表。
返回值:
- 返回一个新的列表。
# 创建空列表
empty_list_literal = []
empty_list_constructor = list()
print(f"空列表 (字面量): {empty_list_literal}")
print(f"空列表 (构造函数): {empty_list_constructor}")
# 从字符串创建列表
char_list = list("hello")
print(f"从字符串创建: {char_list}")
# 从元组创建列表
tuple_to_list = list((1, 2, 3))
print(f"从元组创建: {tuple_to_list}")
# 从字典创建列表(只包含键)
dict_keys_list = list({'a': 1, 'b': 2})
print(f"从字典创建: {dict_keys_list}")
# 从集合创建列表
set_to_list = list({1, 2, 3})
print(f"从集合创建: {set_to_list}")
使用 [] (字面量) 创建空列表通常比 list() (构造函数) 更快,因为它不需要函数调用开销。在性能敏感的代码中,推荐使用 []。
查看列表长度:
# len 查看列表长度
a = [1, 2, 3]
b = [2, 3, 'hello']
c = a + b
print(c) # [1, 2, 3, 2, 3, 'hello']
print(len(c)) # 6
Python 列表和字符串一样可以使用乘法扩展:
d = b * 2
print(d) # [2, 3, 'hello', 2, 3, 'hello']
列表的内置方法
a = [1, 2, 3]
a[0] = 100
print(a) # [100, 2, 3]
# 这种赋值也适用于分片,例如,将列表的第 2,3 两个元素换掉
a[1:3] = [200, 300]
print(a) # [100, 200, 300]
# 事实上,对于连续的分片(即步长为 1 ),Python 采用的是整段替换的方法,两者的元素个数并不需要相同,
# 例如,将 [11,12] 替换为 [1,2,3,4]:
a = [10, 11, 12, 13, 14]
a[1:3] = [1, 2, 3, 4]
print(a) # [10, 1, 2, 3, 4, 13, 14]
# 用这种方法来删除列表中一个连续的分片:
a = [10, 1, 2, 11, 12]
print(a[1:3]) # [1, 2]
a[1:3] = []
print(a) # [10, 11, 12]
# 对于不连续(间隔 step 不为 1)的片段进行修改时,两者的元素数目必须一致:
a = [10, 11, 12, 13, 14]
a[::2] = [1, 2, 3]
print(a) # [1, 11, 2, 13, 3]
Python 提供了删除列表中元素的方法 del:
a = [100, 'a', 'b', 200]
del a[0]
print(a) # ['a', 'b', 200]
# 删除间隔的元素:
a = ['a', 1, 'b', 2, 'c']
del a[::2]
print(a) # [1, 2]
用 in 来看某个元素是否在某个序列(不仅仅是列表)中, 用 not in 来判断是否不在某个序列中。
a = [1, 2, 3, 4, 5]
print(1 in a) # True
print(1 not in a) # False
# 也可以作用于字符串:
s = 'hello world'
print("'he' in s : ", 'he' in s) # True
print("'world' not in s : ", 'world' not in s) # False
列表中可以包含各种对象,甚至可以包含列表:
a = [1, 2, 'six', [3, 4]]
print(a[3]) # [3, 4]
# a[3]是列表,可以对它再进行索引:
print(a[3][1]) # 4
count方法、index方法
# 列表中某个元素个数
a = [1, 1, 2, 3, 4, 5]
print(len(a)) # 总个数:6
# 元素1出现的个数
print(a.count(1)) # 2
# l.index(ob) 返回列表中元素 ob 第一次出现的索引位置,如果 ob 不在 l 中会报错。
print(a.index(1)) # 0
append方法、extend方法、insert方法
# 向列表添加单个元素
# a.append(ob) 将元素 ob 添加到列表 a 的最后。
a = [1, 1, 2, 3, 4, 5]
a.append(10)
print(a) # [1, 1, 2, 3, 4, 5, 10]
# append每次只添加一个元素,并不会因为这个元素是序列而将其展开:
a.append([11, 12])
print(a) # [1, 1, 2, 3, 4, 5, 10, [11, 12]]
# extend方法
# l.extend(lst) 将序列 lst 的元素依次添加到列表 l 的最后,作用相当于 l += lst。
a = [1, 2, 3, 4]
a.extend([6, 7, 1])
print(a) # [1, 2, 3, 4, 6, 7, 1]
# insert方法
# l.insert(idx, ob) 在索引 idx 处插入 ob ,之后的元素依次后移。
a = [1, 2, 3, 4]
# 在索引 3 插入 'a'
a.insert(3, 'a')
print(a) # [1, 2, 3, 'a', 4]
remove方法、pop方法
# l.remove(ob) 会将列表中第一个出现的 ob 删除,如果 ob 不在 l 中会报错。
a = [1, 1, 2, 3, 4]
# 移除第一个1
a.remove(1)
print(a) # [1, 2, 3, 4]
# 弹出元素
# l.pop(idx) 会将索引 idx 处的元素删除,并返回这个元素。
a = [1, 2, 3, 4]
b = a.pop(0) # 1
print('pop:', b, ' ;result:', a)
sort方法、reverse方法
# l.sort() 会将列表中的元素按照一定的规则排序:
a = [10, 1, 11, 13, 11, 2]
a.sort()
print(a) # [1, 2, 10, 11, 11, 13]
# 如果不想改变原来列表中的值,可以使用 sorted 函数(sorted函数不只能应用于列表):
a = [10, 1, 11, 13, 11, 2]
b = sorted(a)
print(a) # [10, 1, 11, 13, 11, 2]
print(b) # [1, 2, 10, 11, 11, 13]
# 列表反向
# list.reverse() 会将列表中的元素从后向前排列。
a = [1, 2, 3, 4, 5, 6]
a.reverse()
print(a) # [6, 5, 4, 3, 2, 1]
如果不想改变原来列表中的值,可以使用切片语法。
a = [1, 2, 3, 4, 5, 6]
b = a[::-1]
print(a) # [1, 2, 3, 4, 5, 6]
print(b) # [6, 5, 4, 3, 2, 1]
if a == b:
print('这是一个回文序列')
clear方法、copy方法
# clear 方法清空列表中的所有元素
a = [1, 2, 3, 4, 5]
a.clear()
print(a) # []
# copy 方法返回列表的浅拷贝
a = [1, 2, 3, 4, 5]
b = a.copy()
b[0] = 100
print(a) # [1, 2, 3, 4, 5]
print(b) # [100, 2, 3, 4, 5]
# copy() 等价于 a[:]
c = a[:]
c[0] = 200
print(a) # [1, 2, 3, 4, 5]
print(c) # [200, 2, 3, 4, 5]
浅拷贝只复制列表本身,不复制列表中的嵌套对象。如果需要完全独立的副本,请使用 copy.deepcopy()。
import copy
a = [[1, 2], [3, 4]]
b = a.copy() # 浅拷贝
c = copy.deepcopy(a) # 深拷贝
b[0][0] = 100
print(a) # [[100, 2], [3, 4]] - 浅拷贝影响原列表
c[0][0] = 200
print(a) # [[100, 2], [3, 4]] - 深拷贝不影响原列表
如果不清楚用法,可以查看帮助: help(a.sort)
a = [1, 2, 3]
help(a.sort)
显示帮助:
# Signature: a.sort(*, key=None, reverse=False)
# Docstring:
# Sort the list in ascending order and return None.
#
# The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
# order of two equal elements is maintained).
#
# If a key function is given, apply it once to each list item and sort them,
# ascending or descending, according to their function values.
#
# The reverse flag can be set to sort in descending order.
# Type: builtin_function_or_method
列表推导式
列表推导式的基本语法结构如下:
[expression for item in iterable]
这相当于:
result = []
for item in iterable:
result.append(expression)
推导式(列表推导式、字典推导式、集合推导式)有自己独立的作用 域:
- i是列表推导式内部的局部变量 - 它只在
[i for i in range(10)]这个表达式内部有效 - 作用域隔离 - 列表推导式的变量不会"泄漏"到外层函数
- 自包含 - 列表推导式可以访问外层作用域的变量,但它自己的循环变量是独立的
常见用法示例:
# 创建平方数列表
squares = [x**2 for x in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# 字符串转大写
words = ['hello', 'world', 'python']
upper_words = [word.upper() for word in words]
print(upper_words) # ['HELLO', 'WORLD', 'PYTHON']
# 提取数字的个位数
numbers = [123, 456, 789]
last_digits = [num % 10 for num in numbers]
print(last_digits) # [3, 6, 9]
带条件的列表推导式,使用if 语句
# 基本语法
[expression for item in iterable if condition]
# 示例:筛选偶数
numbers = range(20)
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 筛选正数并求平方
numbers = [-3, -2, -1, 0, 1, 2, 3]
positive_squares = [x**2 for x in numbers if x > 0]
print(positive_squares) # [1, 4, 9]
# 筛选特定长度的字符串
words = ['cat', 'dog', 'elephant', 'bird', 'python']
long_words = [word for word in words if len(word) > 4]
print(long_words) # ['elephant', 'python']
推导式嵌套
推导式可以嵌套使用,用于处理嵌套的数据结构。从可读性角度,推荐不超过两层嵌套。
# 基本语法
[expression for item1 in iterable1 for item2 in iterable2]
# 示例:生成坐标点
coordinates = [(x, y) for x in range(3) for y in range(3)]
print(coordinates)
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
# 矩阵展平
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]
print(flattened) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 带条件的嵌套循环
result = [(x, y) for x in range(5) for y in range(5) if x + y == 4]
print(result) # [(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)]
# 处理字符串列表
sentences = ['Hello World', 'Python Programming', 'Data Science']
word_lengths = [[len(word) for word in sentence.split()] for sentence in sentences]
print(word_lengths) # [[5, 5], [6, 11], [4, 7]]
# 过滤和转换文件名
filenames = ['data.txt', 'image.jpg', 'script.py', 'document.pdf', 'code.py']
python_files = [filename.upper() for filename in filenames if filename.endswith('.py')]
print(python_files) # ['SCRIPT.PY', 'CODE.PY']
# 处理嵌套数据结构
students = [
{'name': 'Alice', 'scores': [85, 90, 88]},
{'name': 'Bob', 'scores': [78, 85, 92]},
{'name': 'Charlie', 'scores': [92, 88, 95]}
]
averages = [{'name': student['name'], 'average': sum(student['scores'])/len(student['scores'])}
for student in students]
print(averages)
# [{'name': 'Alice', 'average': 87.67}, {'name': 'Bob', 'average': 85.0}, {'name': 'Charlie', 'average': 91.67}]
字典
字典是 Python 中另一种非常重要的数据结构,它是一个无序、可变的键值对(key-value pair)集合。
每个键都必须是唯一的?,并且是hashable(可哈希)的。大部分不可变类型都是可哈希的,如字符串、数字、元组?。
字典推导式让字典的创建变得更加简洁和直观。它遵循了Python"应该有一种,最好只有一种显而易见的方法来做事"的原则。
squares = {x: x**2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
字典方法概览
dict函 数
dict() 函数用于创建一个新的字典。
dict() 函数签名:
dict(**kwargs) -> new dictionarydict(mapping, **kwargs) -> new dictionarydict(iterable, **kwargs) -> new dictionary
参数说明:
**kwargs: 关键字参数,用于创建字典。mapping: 一个映射对象(如另一个字典)。iterable: 一个包含键值对的可迭代对象。
返回值:
- 返回一个新的字典。
# 创建空字典
empty_dict_literal = {}
empty_dict_constructor = dict()
print(f"空字典 (字面量): {empty_dict_literal}")
print(f"空字典 (构造函数): {empty_dict_constructor}")
# 使用关键字参数创建字典
kw_dict = dict(name="Alice", age=30)
print(f"从关键字参数创建: {kw_dict}")
# 从另一个字典创建
mapping_dict = dict({'a': 1, 'b': 2})
print(f"从映射对象创建: {mapping_dict}")
# 从键值对列表创建
iterable_dict = dict([('x', 10), ('y', 20)])
print(f"从可迭代对象创建: {iterable_dict}")
与列表类似,使用 {} (字面量) 创建空字典通常比 dict() 更快。
# 初始化字典
a = {'first': 'num 1', 'second': 'num 2', 3: 'num 3'}
print(a['first']) # num 1
print(a[3]) # num 3
# 插入键值
a['f'] = 'num 1'
a['s'] = 'num 2'
print(a) # {'s': 'num 2', 'f': 'num 1'}
# 查看键值
print(a['s']) # num 2
# 更新
a['f'] = 'num 3'
print(a) # {'s': 'num 2', 'f': 'num 3'}
# 利用索引直接更新键值对
my_dict = {'age': 32}
my_dict['age'] += 1
print(my_dict['age']) # 33
# dict 可以使用元组作为键值
# 例如,可以用元组做键来表示从第一个城市飞往第二个城市航班数的多少:
connections = {}
connections[('New York', 'Seattle')] = 100
connections[('Austin', 'New York')] = 200
connections[('New York', 'Austin')] = 400
# 元组是有序的,
# 因此 ('New York', 'Austin') 和 ('Austin', 'New York') 是两个不同的键:
print(connections[('Austin', 'New York')]) # 200
print(connections[('New York', 'Austin')]) # 400
Python 中不支持用数字索引按顺序查看字典中的值,因为数字本身也有可能成为键值,这样会引起混淆:
try:
print(a[0])
except KeyError as e:
print('KeyError:', e)
# KeyError: 0
字典的值也可以是另一个字典,这样就可以实现嵌套的字典。
# 定义四个字典
e1 = {'mag': 0.05, 'width': 20}
e2 = {'mag': 0.04, 'width': 25}
e3 = {'mag': 0.05, 'width': 80}
e4 = {'mag': 0.03, 'width': 30}
# 以字典作为值传入新的字典
events = {500: e1, 760: e2, 3001: e3, 4180: e4}
import pprint
pprint.pprint(events)
"""
{500: {'mag': 0.05, 'width': 20},
760: {'mag': 0.04, 'width': 25},
3001: {'mag': 0.05, 'width': 80},
4180: {'mag': 0.03, 'width': 30}}
"""
字典的内置方法
get 方法
get 方法 : d.get(key, default = None)
之前已经见过,用索引可以找到一个键对应的值, 但是当字典中没有这个键的时候,Python 会报错
a = {'first': 'num 1', 'second': 'num 2'}
# error:
# print(a['third'])
# get 返回字典中键 key 对应的值,
# 如果没有这个键,返回 default 指定的值(默认是 None )。
print(a.get('third')) # None
# 指定默认值参数:
b = a.get("three", "num 0")
b # num 0
pop 方法删除元素
pop 方法可以用来弹出字典中某个键对应的值,同时也可以指定默认参数:
d.pop(key, default = None)
a = {'first': 'num 1', 'second': 'num 2'}
c = a.pop('first')
print(c) # num 1
print(a) # {u'second': u'num 2'}
# 弹出不存在的键值:
d = a.pop("third", 'not exist')
print(d) # not exist
# 与列表一样,del 函数可以用来删除字典中特定的键值对,例如:
a = {'first': 'num 1', 'second': 'num 2'}
del a["first"]
print(a) # {u'second': u'num 2'}
update 方法更新字典
之前已经知道,可以通过索引来插入、修改单个键值对, 但是如果想对多个键值对进行操作,这种方法就显得比较麻烦,好在有 update 方法:
my_dict = dict([('name', 'lili'),
('sex', 'female'),
('age', 32),
('address', 'beijing')])
# 把 ‘lili' 改成 'lucy',同时插入 'single' 到 'marriage'
dict_update = {'name': 'lucy', 'marriage': 'single'}
my_dict.update(dict_update)
print(my_dict)
import pprint
# {u'marriage': u'single',
# u'name': u'lucy',
# u'address': u'beijing',
# u'age': 32,
# u'sex': u'female'}
pprint.pprint(my_dict) # 华丽丽的显示方式
my_dict # ipython的dict显示跟pprint的格式一样华丽
通过关键词 in 查询字典中是否有该键:
barn = {'cows': 1, 'dogs': 5, 'cats': 3}
# in 可以用来判断字典中是否有某个特定的键:
print('chickens' in barn) # False
print('cows' in barn) # True
keys 方法,values 方法和 items 方法
d.keys()返回一个包含所有键的字典视图对象;d.values()返回一个包含所有值的字典视图对象;d.items()返回一个包含所有键值对的字典视图对象;
barn = {'cows': 1, 'dogs': 5, 'cats': 3}
print(barn.keys()) # dict_keys(['cows', 'dogs', 'cats'])
print(barn.values()) # dict_values([1, 5, 3])
print(barn.items()) # dict_items([('cows', 1), ('dogs', 5), ('cats', 3)])
# 视图对象是动态的,会反映字典的变化
barn['sheep'] = 2
print(barn.keys()) # dict_keys(['cows', 'dogs', 'cats', 'sheep'])
# 如果需要列表,可以转换
keys_list = list(barn.keys())
print(keys_list) # ['cows', 'dogs', 'cats', 'sheep']
setdefault 方法
setdefault 方法用于获取指定键的值,如果键不存在,则插入该键并设置默认值。
# d.setdefault(key, default=None)
# 如果 key 存在,返回对应的值
# 如果 key 不存在,插入 key 并设置值为 default,然后返回 default
person = {'name': 'Alice', 'age': 30}
# 键存在,返回对应值
print(person.setdefault('name', 'Unknown')) # Alice
# 键不存在,插入并返回默认值
print(person.setdefault('city', 'Beijing')) # Beijing
print(person) # {'name': 'Alice', 'age': 30, 'city': 'Beijing'}
# 不指定默认值时,默认为 None
print(person.setdefault('country')) # None
print(person) # {'name': 'Alice', 'age': 30, 'city': 'Beijing', 'country': None}
setdefault 常用于处理字典值为列表或集合的情况,避免重复检查键是否存在:
# 统计单词出现位置
text = ['apple', 'banana', 'apple', 'cherry', 'banana']
positions = {}
for i, word in enumerate(text):
positions.setdefault(word, []).append(i)
print(positions) # {'apple': [0, 2], 'banana': [1, 4], 'cherry': [3]}
popitem 方法、clear 方法
# popitem 删除并返回字典中的最后一个键值对(Python 3.7+ 按插入顺序)
# 如果字典为空,会抛出 KeyError
d = {'a': 1, 'b': 2, 'c': 3}
item = d.popitem()
print(item) # ('c', 3)
print(d) # {'a': 1, 'b': 2}
# clear 方法清空字典中的所有元素
d.clear()
print(d) # {}
在 Python 3.7 之前,popitem 会删除并返回一个任意的键值对。从 Python 3.7 开始,字典保持插入顺序,popitem 删除最后插入的项。
copy 方法、fromkeys 方法
# copy 方法返回字典的浅拷贝
original = {'a': 1, 'b': 2, 'c': 3}
copied = original.copy()
copied['a'] = 100
print(original) # {'a': 1, 'b': 2, 'c': 3}
print(copied) # {'a': 100, 'b': 2, 'c': 3}
# fromkeys 类方法用指定的键创建新字典,所有键的值都设为相同的默认值
keys = ['name', 'age', 'city']
new_dict = dict.fromkeys(keys)
print(new_dict) # {'name': None, 'age': None, 'city': None}
# 指定默认值
new_dict2 = dict.fromkeys(keys, 'Unknown')
print(new_dict2) # {'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'}
# 从另一个字典的键创建新字典
template = original.fromkeys(original.keys(), 0)
print(template) # {'a': 0, 'b': 0, 'c': 0}
copy() 是浅拷贝,对于嵌套的可变对象,内层对象仍然是引用:
import copy
d = {'list': [1, 2, 3], 'value': 10}
d_copy = d.copy() # 浅拷贝
d_deep = copy.deepcopy(d) # 深拷贝
d_copy['list'][0] = 100
print(d) # {'list': [100, 2, 3], 'value': 10}
print(d_copy) # {'list': [100, 2, 3], 'value': 10}
print(d_deep) # {'list': [1, 2, 3], 'value': 10}
字典推导式
基本语法
{key_expression: value_expression for item in iterable}
示例
# 创建平方数字典
squares_dict = {x: x**2 for x in range(5)}
print(squares_dict) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
# 字符串长度字典
words = ['apple', 'banana', 'cherry']
word_lengths = {word: len(word) for word in words}
print(word_lengths) # {'apple': 5, 'banana': 6, 'cherry': 6}
# 反转字典
original = {'a': 1, 'b': 2, 'c': 3}
reversed_dict = {v: k for k, v in original.items()}
print(reversed_dict) # {1: 'a', 2: 'b', 3: 'c'}
带条件的字典推导式
# 筛选偶数键值对
numbers = range(10)
even_squares = {x: x**2 for x in numbers if x % 2 == 0}
print(even_squares) # {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
# 过滤字典
scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78, 'Diana': 96}
high_scores = {name: score for name, score in scores.items() if score >= 90}
print(high_scores) # {'Bob': 92, 'Diana': 96}
# 条件值设置
numbers = range(-3, 4)
abs_dict = {x: abs(x) if x < 0 else x for x in numbers}
print(abs_dict) # {-3: 3, -2: 2, -1: 1, 0: 0, 1: 1, 2: 2, 3: 3}
# 统计字符频率
text = "hello world"
char_count = {char: text.count(char) for char in set(text) if char != ' '}
print(char_count) # {'e': 1, 'h': 1, 'l': 3, 'o': 2, 'r': 1, 'd': 1, 'w': 1}
# 分组数据
students = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve']
grouped = {len(name): [n for n in students if len(n) == len(name)] for name in students}
# 去重
grouped = {length: list(set(names)) for length, names in grouped.items()}
print(grouped) # {5: ['Alice', 'Diana'], 3: ['Bob', 'Eve'], 7: ['Charlie']}
# 嵌套字典处理
data = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'London'},
{'name': 'Charlie', 'age': 35, 'city': 'Tokyo'}
]
name_to_info = {person['name']: {k: v for k, v in person.items() if k != 'name'}
for person in data}
print(name_to_info)
# {'Alice': {'age': 25, 'city': 'New York'}, 'Bob': {'age': 30, 'city': 'London'}, 'Charlie': {'age': 35, 'city': 'Tokyo'}}
集合
集合是 Python 中的一种无序、可变的数据结构,它只包含唯一的元素。集合中的元素必须是不可变类型。
集合字面量语法让集合的创建变得更加直观和高效。使用花括号{}创建集合,体现了Python"应该有一种显而易见的方法来做事"的原则。
# 旧方式
colors = set(['red', 'green', 'blue'])
# 新语法 - 更清晰直观
colors = {'red', 'green', 'blue'}
# 集合推导式
squares = {x**2 for x in range(5)}
集合方法概览
frozenset函数
frozenset 是 set 的不可变版本。一旦创建,frozenset 的元素就不能被修改。这使得 frozenset 可以作为字典的键或集合中的元素。
frozenset() 函数用于创建一个新的不可变集合。
frozenset() 函数签名: frozenset([iterable]) -> new frozenset
参数说明:
iterable(可选): 一个可迭代对象。
返回值:
- 返回一个新的不可变集合。
# 创建一个 frozenset
frozen = frozenset([1, 2, 3, 4, 5])
print(frozen)
# frozenset 可以作为字典的键
data = {frozen: 'my_frozen_set'}
print(data[frozen])
# frozenset 支持所有不修改集合的 set 方法
other_set = {4, 5, 6, 7}
print(frozen.difference(other_set)) # frozenset({1, 2, 3})
由于 frozenset 是不可变的,它不支持 add, remove, pop, update 等会修改集合内容的方法。
set函数
set() 函数用于创建一个新的集合。
set() 函数签名: set([iterable]) -> new set
参数说明:
iterable(可选): 一个可迭代对象,如列表、元组、字符串等。
返回值:
- 返回一个新的集合。
# 创建空集合
empty_set = set()
print(f"空集合: {empty_set}")
# 从列表创建集合(自动去重)
list_to_set = set([1, 2, 2, 3])
print(f"从列表创建: {list_to_set}")
# 从字符串创建集合
string_to_set = set("hello")
print(f"从字符串创建: {string_to_set}")
创建空集合时必须使用 set(),因为 {} 创建的是一个空字典。
empty_dict = {}
print(type(empty_dict)) # <class 'dict'>
# 使用一个列表来初始化一个集合:
a = set([1, 2, 3, 1])
a # 集合会自动去除重复元素 1。
# 集合中的元素是用大括号{}包含起来的,这意味着可以用{}的形式来创建集合:
a = {1, 2, 3, 1}
print(a) # {1, 2, 3}
# 但是创建空集合的时候只能用set来创建,因为在Python中{}创建的是一个空的字典:
s = {}
print(type(s)) # <type 'dict'>