Python - 不可變資料結構



Python 的不可變資料結構是指一旦建立後就無法更改的資料結構。這意味著任何嘗試修改資料結構的操作都將導致建立一個新的例項,而不是修改原始例項。不可變資料結構對於確保資料在程式執行過程中保持不變非常有用,這有助於防止錯誤並使程式碼更易於理解和維護。

在深入探討這個主題之前,讓我們快速回顧一下什麼是資料結構?資料結構是用於組織、處理、檢索和儲存資料的特殊格式。它們定義了資料在記憶體中的排列方式以及如何有效地執行諸如訪問、插入、刪除和更新等操作。

Python 中不同的不可變資料結構

不可變資料結構在 Python 中因其穩定性、執行緒安全性以及易用性而至關重要。以下是 Python 中不同的不可變資料結構:

  • 元組:這些是有序的專案集合,建立後無法更改。它們可以包含混合資料型別,並且對於表示固定相關的專案集合很有用。
  • 字串:這些資料結構是字元序列,並且是不可變的。任何修改字串的操作都將建立一個新的字串。
  • 凍結集:這些是集合的不可變版本。與普通集合不同,凍結集在建立後不允許修改。
  • 命名元組:這些是元組的子類,具有命名欄位,可以提供更具可讀性和自文件化的程式碼。它們像普通元組一樣是不可變的。

現在,讓我們詳細介紹每個不可變資料結構。

元組

Python 中的元組是不可變的元素序列,這意味著一旦建立,它們就無法修改。它們使用括號 '()' 定義,並且可以儲存各種專案,例如數字、字串甚至其他元組。

建立元組

元組使用括號 '()' 建立,元素之間用逗號 ',' 分隔。即使只有一個元素的元組也需要尾隨逗號來將其與分組表示式區分開來。

以下是透過將括號 '()' 賦值給變數來建立元組的示例:

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, 'Tutorialspoint', 3.14)
print("Multi elements tuple:", multi_element_tuple)
nested_tuple = (1, (2, 3), 'Learning')
print("Nested tuple:", nested_tuple)

執行上述程式碼後,我們將得到以下輸出

Single element tuple: (5,)
Multi elements tuple: (1, 2, 'Tutorialspoint', 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 does not support item assignment
Error: 'tuple' object has no attribute 'append'
Original tuple: (4, 5, 6)

字串

Python 中的字串是字元序列,用於表示和操作文字資料。它們用單引號'或雙引號"括起來,也可以使用三引號"""來表示多行字串。

主要特徵包括不可變性,即建立後字串無法更改;有序索引,可以透過位置訪問字元;以及支援各種操作,例如連線、切片和迭代。

字串在 Python 中是基礎資料型別,用於文字處理、輸入/輸出操作以及應用程式中的資料表示,提供了一套功能強大的工具集,以及用於高效操作和格式化文字資訊的內建方法。

建立字串

每種字串建立方法(即 ', ", """)都有其自身的用例,具體取決於我們是否需要在字串中包含引號、處理多行文字或在 Python 程式碼中滿足其他特定格式要求。

以下是使用三種引號 ', ", """ 建立字串的示例:

# Single line string
single_quoted_string = 'Hello, Welcome to Tutorialspoint'

# 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)

執行上述程式碼後,我們將得到以下輸出

Hello, Welcome to Tutorialspoint
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 Learners
Hello
Error: 'str' object does not support item assignment

凍結集

Python 中的凍結集是集合的不可變版本。建立後,其元素不能更改、新增或刪除。凍結集在我們需要一個在程式執行過程中保持不變的集合的情況下特別有用,尤其是在我們想要將其用作字典的鍵或另一個集合的元素時。

建立凍結集

我們可以使用frozenset()建構函式建立凍結集,並將可迭代物件(如列表或其他集合)作為引數傳遞。以下是建立凍結集的示例:

# Creating a frozen set
fset = frozenset([1, 2, 3, 4])

# Printing the frozen set
print(fset)  

執行上述程式碼後,我們將得到以下輸出

frozenset({1, 2, 3, 4})

理解 Python 中凍結集的不可變性

以下示例展示了凍結集的不可變性,以及建立後不允許修改。

# 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 'add'
Error: 'frozenset' object has no attribute 'remove'
Original frozenset: frozenset({1, 2, 3, 4})

命名元組

Python 中的命名元組是 collections 模組中提供的一種輕量級資料結構,其行為類似於元組,但允許我們使用命名屬性以及索引來訪問其元素。

它結合了元組的優點(例如不可變、記憶體效率)以及透過名稱引用元素的能力,從而提高了程式碼的可讀性和可維護性。

建立命名元組

我們可以使用 collections 模組中的namedtuple()工廠函式定義命名元組。它接受兩個引數:命名元組型別的名稱和欄位名稱的序列(即字串或字串的可迭代物件),用於指定其欄位的名稱。

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
1
2

理解 Python 中命名元組的不可變性

Python 中的命名元組由collections.namedtuple工廠函式提供,確實是不可變的。它們的行為類似於普通元組,但具有命名欄位,使其更具可讀性和自文件性。

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) 

執行上述程式碼後,我們將得到以下輸出

Point(x=1, y=2)
Error occurred: can't set attribute
1
2
廣告