Python 不可变数据结构是一旦创建就无法更改的数据结构。这意味着任何修改数据结构的尝试都将导致创建新实例,而不是更改原始实例。不可变数据结构可用于确保数据在整个程序执行过程中保持不变,这有助于防止错误并使代码更易于理解和维护。
在深入探讨这个主题之前,让我们快速回顾一下 什么是数据结构?
数据结构是用于组织、处理、检索和存储数据的专用格式。它们定义了数据在内存中的排列方式,以及如何有效地执行访问、插入、删除和更新等操作。
Python 中不同的不可变数据结构
不可变数据结构在 Python 中因其稳定性、线程安全性和易用性而至关重要。以下是 Python 中不同的不可变数据结构 -
- Tuples:这些是项目的有序集合,这些项目在创建后无法更改。它们可以包含混合数据类型,并且可用于表示相关项的固定集合。
- Strings:这些数据结构是字符序列,并且是不可变的。任何修改字符串的操作都将创建一个新字符串。
- Frozensets:这些是 sets 的不可变版本。与常规集不同,frozen set 不允许在创建后进行修改。
- Named Tuples:这些是具有命名字段的 Tuples 的子类,可提供更具可读性和自文档化的代码。它们像常规元组一样是不可变的。
现在,让我们详细介绍每个 Immutable 数据结构。
元组( Tuples)
Python 中的 Tuples 是不可变的元素序列,这意味着一旦创建,就无法修改它们。它们使用括号 '()' 定义,可以保存项目集合,例如数字、字符串甚至其他元组。
创建 Tuples
Tuples使用括号 '()' 和用逗号分隔的元素 ',' 创建。即使是具有单个元素的元组也需要一个尾随逗号来将它们与分组表达式区分开来。
以下是通过将括号 '()' 分配给变量来创建 Tuples 的示例 -
empty_tuple = ()
single_element_tuple = (5,) # Note the comma after the single element
print("Single element tuple:", single_element_tuple)
multi_element_tuple = (1, 2, 'qikepu', 3.14)
print("Multi elements tuple:", multi_element_tuple)
nested_tuple = (1, (2, 3), 'Learning')
print("Nested tuple:", nested_tuple)
在执行上述代码时,我们将得到以下输出 -
Multi elements tuple: (1, 2, 'qikepu', 3.14)
Nested tuple: (1, (2, 3), 'Learning')
了解 Python 中的元组不可变性
在这里,我们将了解 python 中元组的不变性。下面是示例 -
# Define a tuple
my_tuple = (1, 2, 3, 'hello')
# Attempt to modify an element (which is not possible with tuples)
# This will raise a TypeError
try:
my_tuple[0] = 10
except TypeError as e:
print(f"Error: {e}")
# Even trying to append or extend a tuple will result in an error
try:
my_tuple.append(4)
except AttributeError as e:
print(f"Error: {e}")
# Trying to reassign the entire tuple to a new value is also not allowed
try:
my_tuple = (4, 5, 6)
except TypeError as e:
print(f"Error: {e}")
print("Original tuple:", my_tuple)
在执行上述代码时,我们将得到以下输出 -
Error: 'tuple' object has no attribute 'append'
Original tuple: (4, 5, 6)
Strings
Python 中的 Strings 是用于表示和操作文本数据的字符序列。它们括在单引号 ' 或双引号 “ 中,并可选择对多行字符串使用三引号 ”“”。
主要特征包括不变性(这意味着一旦创建这些字符串就无法更改)、按位置访问字符的有序索引以及对各种操作(如连接、切片和迭代)的支持。
字符串是 Python 中的基础,用于文本处理、输入/输出操作和数据表示等任务,为应用程序提供具有内置方法的多功能工具集,用于高效操作和格式化文本信息。
创建 Strings
每种类型的字符串创建方法(即 '、 “、” “”“)都有自己的用例,具体取决于我们是否需要在字符串中包含引号、处理多行文本或在我们的 Python 代码中满足其他特定的格式要求。
以下是在三种类型的引号 ', “、” “” 的帮助下创建字符串的示例 −
# Single line string
single_quoted_string = 'Hello, Welcome to qikepu'
# Double quoted string
double_quoted_string = "Python Programming"
# Triple quoted string for multi-line strings
multi_line_string = """This is a
multi-line
string"""
print(single_quoted_string)
print(double_quoted_string)
print(multi_line_string)
在执行上述代码时,我们将得到以下输出 -
Python Programming
This is a
multi-line
string
了解 Python 中的字符串不可变性
在以下示例的帮助下,我们将了解 python 中字符串的不变性。
# Example demonstrating string immutability
my_string = "Hello"
# Attempting to modify a string will create a new string instead of modifying the original
modified_string = my_string + " Learners"
print(modified_string) # Output: Hello Learners
# Original string remains unchanged
print(my_string) # Output: Hello
# Trying to modify the string directly will raise an error
try:
my_string[0] = 'h' # TypeError: 'str' object does not support item assignment
except TypeError as e:
print(f"Error: {e}")
在执行上述代码时,我们将得到以下输出 -
Hello
Error: 'str' object does not support item assignment
Frozen sets
Python 中的 Frozen sets 是集的不可变版本。一旦创建,其元素就无法更改、添加或删除。 Frozen sets 在我们需要一个在整个程序执行过程中保持不变的集合的情况下特别有用,特别是当我们想将其用作字典中的键或另一个集中的元素时。
创建 Frozen sets
我们可以使用 frozenset() 构造函数创建一个 Frozen sets ,方法是将可迭代对象(例如列表或其他集)作为参数传递。以下是创建 Frozen sets 的示例 -
# Creating a frozen set
fset = frozenset([1, 2, 3, 4])
# Printing the frozen set
print(fset)
在执行上述代码时,我们将得到以下输出 -
了解 Python 中的 Frozen sets 不变性
以下示例显示了 frozen sets 如何保持不可变,并且在创建后不允许修改。
# Creating a frozenset
frozen_set = frozenset([1, 2, 3, 4])
# Attempting to add an element to the frozenset will raise an error
try:
frozen_set.add(5)
except AttributeError as e:
print(f"Error: {e}")
# Attempting to remove an element from the frozenset will also raise an error
try:
frozen_set.remove(2)
except AttributeError as e:
print(f"Error: {e}")
# The original frozenset remains unchanged
print("Original frozenset:", frozen_set)
在执行上述代码时,我们将得到以下输出 -
Error: 'frozenset' object has no attribute 'remove'
Original frozenset: frozenset({1, 2, 3, 4})
Named Tuples
Python 中的 Named Tuples 是 collections 模块中提供的一种轻量级数据结构,其行为与元组相同,但允许我们使用命名属性和索引访问其元素。
它结合了元组的优点,例如不可变、内存效率高,并且能够按名称引用元素,从而提高了代码的可读性和可维护性。
创建 Named Tuples
我们可以使用 collections 模块中的 namedtuple() 工厂函数定义一个命名元组。它需要两个参数,例如命名 Tuples 类型的 name 和一个 sequence,即字段名称的字符串或指定其字段名称的字符串的可迭代对象。
from collections import namedtuple
# Define a named tuple type 'Point' with fields 'x' and 'y'
Point = namedtuple('Point', ['x', 'y'])
# Create an instance of Point
p1 = Point(1, 2)
# Access elements by index (like a tuple)
print(p1[0])
# Access elements by name
print(p1.x)
print(p1.y)
在执行上述代码时,我们将得到以下输出 -
1
2
了解 Python 中的 Named Tuples 不可变性
Python 中的 Named Tuples 由 collections.namedtuple 提供,工厂函数确实是不可变的。它们的行为类似于常规 Tuples,但通过使字段更具可读性和自文档性来命名字段。
from collections import namedtuple
# Define a named tuple called Point with fields 'x' and 'y'
Point = namedtuple('Point', ['x', 'y'])
# Create an instance of Point
p = Point(x=1, y=2)
print(p)
# Attempt to modify the named tuple
# This will raise an AttributeError since named tuples are immutable
try:
p.x = 10
except AttributeError as e:
print(f"Error occurred: {e}")
# Accessing elements in a named tuple is similar to accessing elements in a regular tuple
print(p.x)
print(p.y)
在执行上述代码时,我们将得到以下输出 -
Error occurred: can't set attribute
1
2