原文:Python Dictionary Methods – Dictionaries in Python,作者:Kolade Chris

在 Python 中,字典是核心数据结构之一。它是由逗号分隔并用花括号括起来的键值对序列。

keyvalue

如果你熟悉 JavaScript,那么 Python 字典就像 JavaScript 对象。

Python 提供了十多种使用字典的方法。

在本文中,我将向你展示如何在 Python 中创建字典并使用这些方法操作它。

我们将涵盖的内容

  • 如何在 Python 中创建字典
  • 操作 Python 字典的方法
  • 如何使用 get() 字典方法
  • 如何使用 items() 字典方法
  • 如何使用 keys() 字典方法
  • 如何使用 values() 字典方法
  • 如何使用 pop() 字典方法
  • 如何使用 popitem() 字典方法
  • 如何使用 update() 字典方法
  • 如何使用 copy() 字典方法
  • 如何使用 clear() 字典方法
  • 小结

如何在 Python 中创建字典

要创建字典,你需要使用一个花括号,并将数据放在一个以逗号分隔的键值对中。

字典的基本语法如下所示:

demo_dict = {
"key1": "value1",
"key2": "value2", 
"key3": "value3"
}

请注意,值可以是任何数据类型并且可以重复,但键不能重复。如果键重复,你将收到语法错误 invalid(无效)。

操作 Python 字典的方法

我将使用下面的字典向你展示字典方法的工作原理:

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

如何使用 get() 字典方法

get() 方法返回指定键的值。

在下面的代码中,我能够通过在 get() 方法中传递 founder 键来获取 freeCodeCamp 的创始人:

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

founder = first_dict.get("founder")
print(founder)

# Output: Quincy Larson

如何使用 items() 字典方法

items() 方法以列表的形式返回字典的所有条目。列表中有一个表示每个项目的元组。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

items = first_dict.items()
print(items)

# Output: dict_items([('name', 'freeCodeCamp'), ('founder', 'Quincy Larson'), ('type', 'charity'), ('age', 8), ('price', 'free'), ('work-style', 'remote')])

如何使用 keys() 字典方法

keys() 返回字典中的所有键。它也返回元组中的键。元组是另一种 Python 数据结构。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

dict_keys = first_dict.keys()
print(dict_keys)

# Output: dict_keys(['name', 'founder', 'type', 'age', 'price', 'work-style'])

如何使用 values() 字典方法

values() 方法访问字典中的所有值。与 keys() 方法一样,它也返回元组中的值。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

dict_values = first_dict.values()
print(dict_values)

# Output: dict_values(['freeCodeCamp', 'Quincy Larson', 'charity', 8, 'free', 'remote'])

如何使用 pop() 字典方法

pop() 方法从字典中删除一个键值对。要使其工作,你需要在其括号内指定键。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

first_dict.pop("work-style")
print(first_dict)

# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free'}

你可以看到 work-style 键及其值已从字典中被删除。

如何使用 popitem() 字典方法

popitem() 方法的工作方式与 pop() 方法类似,不同之处在于它删除了字典中的最后一项。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

first_dict.popitem()
print(first_dict)

# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free'}

你可以看到最后一个键值对(“work-style”:“remote”)已从字典中被删除。

如何使用 update() 字典方法

update() 方法将一个项目添加到字典中。你必须在其括号内指定键和值,并用花括号将其括起来。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

first_dict.update({"Editor": "Abbey Rennemeyer"})
print(first_dict)

# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free', 'work-style': 'remote', 'Editor': 'Abbey Rennemeyer'}

新条目已被添加到字典中。

如何使用 copy() 字典方法

copy() 方法正如其名称所暗示的那样——它将字典复制到指定的变量中。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

second_dict = first_dict.copy()
print(second_dict)

# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free', 'work-style': 'remote'}

如何使用 clear() 字典方法

clear() 方法删除字典中的所有条目。

first_dict = {
    "name": "freeCodeCamp", 
    "founder": "Quincy Larson",
    "type": "charity", 
    "age": 8, 
    "price": "free", 
    "work-style": "remote",
}

first_dict.clear()
print(first_dict)

# Output: {}

小结

在本文中,你学习了如何创建 Python 字典以及如何使用 Python 提供的内置方法来操作字典。

如果你觉得这篇文章有帮助,请不要犹豫,与朋友和家人分享它。

继续编码:)