Python 简明教程

Python - Loop Dictionaries

Loop Through Dictionaries

在 Python 中遍历字典是指迭代字典中的键值对并对每对执行操作。这便于访问键及其对应的值。有几种方法/方法可用于遍历字典 −

Looping through dictionaries in Python refers to iterating over key-value pairs within the dictionary and performing operations on each pair. This allows you to access both keys and their corresponding values. There are several ways/methods for looping through dictionaries −

  1. Using a for Loop

  2. Using dict.items() method

  3. Using dict.keys() method

  4. Using dict.values() method

Loop Through Dictionary Using a For Loop

Python 中的 for 循环是一个控制流语句,它迭代一系列元素。它对序列中的每一个项目重复执行一个代码块。该序列可以是数字范围、列表、元组、字符串或任何可迭代对象。

A for loop in Python is a control flow statement that iterates over a sequence of elements. It repeatedly executes a block of code for each item in the sequence. The sequence can be a range of numbers, a list, a tuple, a string, or any iterable object.

我们可以通过迭代字典中的键或键值对来使用 Python 中的 for 循环遍历字典。有两种常见途径 −

We can loop through dictionaries using a for loop in Python by iterating over the keys or key-value pairs within the dictionary. There are two common approaches −

Example: Iterating over Keys

此方法中,该循环迭代字典的键。在循环内部,你可以使用字典索引访问对应于每把键的值 −

In this approach, the loop iterates over the keys of the dictionary. Inside the loop, you can access the value corresponding to each key using dictionary indexing −

student = {"name": "Alice", "age": 21, "major": "Computer Science"}
for key in student:
   print(key, student[key])

它将生成如下输出:

It will produce the following output −

name Alice
age 21
major Computer Science

Example: Iterating over Key-Value Pairs

此途径中,该循环迭代键值对并使用字典的 items() 方法。每一次迭代提供该键及其对应值 −

In this approach, the loop iterates over the key-value pairs using the items() method of the dictionary. Each iteration provides both the key and its corresponding value −

student = {"name": "Alice", "age": 21, "major": "Computer Science"}
for key, value in student.items():
   print(key, value)

我们得到了如下输出 −

We get the output as shown below −

name Alice
age 21
major Computer Science

Loop Through Dictionary Using dict.items() Method

Python 中的 dict.items() 方法用于返回一个视图对象,该对象显示字典中的键值对列表。该视图对象提供字典项目的动态视图,允许访问其键及其对应值。

The dict.items() method in Python is used to return a view object that displays a list of key-value pairs in the dictionary. This view object provides a dynamic view of the dictionary’s items, allowing you to access both the keys and their corresponding values.

我们可以通过迭代该方法返回的键值对使用 dict.items() 方法来遍历字典。

We can loop through dictionaries using the dict.items() method by iterating over the key-value pairs returned by this method.

Example

此例中,在 "student" 字典中调用了 items() 方法,返回包含键值对的视图对象。for 循环迭代每一对,将键分配给变量 "key",将对应值分配给变量 "value" −

In this example, the items() method is called on the "student" dictionary, returning a view object containing the key-value pairs. The for loop iterates over each pair, assigning the key to the variable "key" and the corresponding value to the variable "value" −

student = {"name": "Alice", "age": 21, "major": "Computer Science"}

# Looping through key-value pairs
for key, value in student.items():
   print(key, value)

下面显示了产生的输出:

The output produced is as shown below −

name Alice
age 21
major Computer Science

Loop Through Dictionary Using dict.keys() Method

Python 中的 dict.keys() 方法用于返回一个视图对象,该对象显示字典中的键列表。该视图对象提供字典键的动态视图,允许访问并迭代它们。

The dict.keys() method in Python is used to return a view object that displays a list of keys in the dictionary. This view object provides a dynamic view of the dictionary’s keys, allowing you to access and iterate over them.

我们可以通过迭代此方法返回的键使用 dict.keys() 方法来遍历字典。这便于访问并迭代字典的键。

We can loop through dictionaries using the dict.keys() method by iterating over the keys returned by this method. This allows us to access and iterate over the keys of the dictionary.

Example

在以下示例中,在 "student" 字典中调用 keys() 方法,返回包含键的视图对象。for 循环迭代视图对象中的每个键,允许在每一次迭代中基于字典的键执行操作 −

In the example below, the keys() method is called on the "student" dictionary, returning a view object containing the keys. The for loop iterates over each key in the view object, allowing you to perform operations based on the keys of the dictionary during each iteration −

student = {"name": "Alice", "age": 21, "major": "Computer Science"}

# Looping through keys
for key in student.keys():
   print(key)

以下是上面代码的输出: -

Following is the output of the above code −

name
age
major

Loop Through Dictionary Using dict.values() Method

Python 中的 dict.values() 方法用于返回一个视图对象,该对象显示字典中的值列表。该视图对象提供字典值的动态视图,允许访问并迭代它们。

The dict.values() method in Python is used to return a view object that displays a list of values in the dictionary. This view object provides a dynamic view of the dictionary’s values, allowing you to access and iterate over them.

我们可以通过迭代此方法返回的值来使用 dict.values() 方法循环遍历字典。这允许我们访问和迭代字典的值。

We can loop through dictionaries using the dict.values() method by iterating over the values returned by this method. This allows us to access and iterate over the values of the dictionary.

Example

在以下示例中,在 “student” 字典中调用 values() 方法,返回一个包含 − 值的视图对象

In the following example, the values() method is called on the "student" dictionary, returning a view object containing the values −

student = {"name": "Alice", "age": 21, "major": "Computer Science"}

# Looping through values
for value in student.values():
   print(value)

以上代码的输出如下所示 −

Output of the above code is as shown below −

Alice
21
Computer Science