Python - 更改字典項



更改字典項

在 Python 中更改字典項是指修改字典中與特定鍵關聯的值。這可能包括更新現有鍵的值、新增新的鍵值對或從字典中刪除鍵值對。

字典是可變的,這意味著在建立後可以修改其內容。

修改字典值

修改 Python 字典中的值是指更改與現有鍵關聯的值。為此,您可以直接將新值賦給該鍵。

示例

在以下示例中,我們定義了一個名為“person”的字典,其中包含鍵“name”、“age”和“city”及其對應值。然後,我們將與鍵“age”關聯的值修改為 26 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Modifying the value associated with the key 'age'
person['age'] = 26
print(person)

它將產生以下輸出 -

{'name': 'Alice', 'age': 26, 'city': 'New York'}

更新多個字典值

如果您需要一次更新字典中的多個值,可以使用 update() 方法。此方法用於使用來自另一個字典或鍵值對的可迭代物件的元素來更新字典。

update() 方法將提供的字典或可迭代物件中的鍵值對新增到原始字典中,如果它們已存在於原始字典中,則用新值覆蓋任何現有鍵。

示例

在下面的示例中,我們使用 update() 方法修改“persons”字典中與鍵“age”和“city”關聯的值 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Updating multiple values
person.update({'age': 26, 'city': 'Los Angeles'})
print(person)

我們得到的輸出如下所示 -

{'name': 'Alice', 'age': 26, 'city': 'Los Angeles'}

條件字典修改

Python 字典中的條件修改是指僅在滿足特定條件時才更改與鍵關聯的值。

您可以使用 if 語句檢查特定條件是否為真,然後再修改與鍵關聯的值。

示例

在此示例中,如果“persons”字典中鍵“age”的當前值為“25”,則將其關聯的值有條件地修改為“26” -

# Initial dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Conditionally modifying the value associated with 'age'
if person['age'] == 25:
   person['age'] = 26
print(person)

獲得的輸出如下所示 -

{'name': 'Alice', 'age': 26, 'city': 'New York'}

透過新增新的鍵值對修改字典

將新的鍵值對新增到 Python 字典中是指將新的鍵及其對應值插入到字典中。

此過程允許您根據需要透過包含其他資訊來動態擴充套件儲存在字典中的資料。

示例:使用賦值運算子

您可以透過直接將值賦給新鍵來將新的鍵值對新增到字典中,如下所示。在下面的示例中,鍵“city”與值“New York”一起新增到“person”字典中 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25}
# Adding a new key-value pair 'city': 'New York'
person['city'] = 'New York'
print(person)

產生的結果如下 -

{'name': 'Alice', 'age': 25, 'city': 'New York'}

示例:使用 setdefault() 方法

您可以使用 setdefault() 方法將新的鍵值對新增到字典中,前提是該鍵尚不存在。

在此示例中,setdefault() 方法僅當鍵“city”尚不存在時才將新鍵“city”與值“New York”一起新增到“person”字典中 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25}
# Adding a new key-value pair 'city': 'New York'
person.setdefault('city', 'New York')
print(person)

以下是上述程式碼的輸出 -

{'name': 'Alice', 'age': 25, 'city': 'New York'}

透過刪除鍵值對修改字典

從 Python 字典中刪除鍵值對是指從字典中刪除特定的鍵及其對應值。

此過程允許您根據要消除的鍵選擇性地從字典中刪除資料。

示例:使用 del 語句

您可以使用 del 語句從字典中刪除特定的鍵值對。在此示例中,del 語句從“person”字典中刪除鍵“age”及其關聯的值 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Removing the key-value pair associated with the key 'age'
del person['age']
print(person)

上述程式碼的輸出如下所示 -

{'name': 'Alice', 'city': 'New York'}

示例:使用 pop() 方法

您還可以使用pop()方法從字典中刪除特定的鍵值對,並返回與已刪除鍵關聯的值。

在這裡,pop()方法從“person”字典中刪除鍵“age”及其關聯的值 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Removing the key-value pair associated with the key 'age'
removed_age = person.pop('age')

print(person)
print("Removed age:", removed_age)

它將產生以下輸出 -

{'name': 'Alice', 'city': 'New York'}
Removed age: 25

示例:使用popitem()方法

您也可以使用popitem()方法刪除字典中的最後一個鍵值對,並將其作為元組返回。

現在,popitem()方法從“person”字典中刪除最後一個鍵值對,並將其作為元組返回 -

# Initial dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Removing the last key-value pair 
removed_item = person.popitem()

print(person)
print("Removed item:", removed_item)

我們得到的輸出如下所示 -

{'name': 'Alice', 'age': 25}
Removed item: ('city', 'New York')
廣告