Python 如何遍历字典
字典是Python中有价值且经常使用的数据结构。本文告诉我们如何遍历字典并对其键值对执行操作。
使用dict.items()方法
Python的dict.items()方法允许您遍历字典。每次迭代将为您提供每个项的键和值。
示例
以下是使用dict.items()方法遍历字典的示例:
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for keys, values in dictionary.items():
print(keys ,values)
输出
以下是上述代码的输出结果。
Novel Pride and Prejudice
year 1813
author Jane Austen
character Elizabeth Bennet
使用Keys()方法
为了遍历字典的键,可以利用字典提供的keys()方法。该方法返回字典中可用的键的可迭代对象。然后,如下所示,可以使用for循环循环遍历键。
示例1
以下是使用keys()方法对字典进行遍历的示例-
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for keys in dictionary.keys():
print(keys)
输出
以下是上述代码的输出:
Novel
year
author
character
示例2
如果你想在每次迭代中获取键的值,可以使用 get() 方法,就像下面的示例一样:
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for keys in dictionary.keys():
print(dictionary.get(keys))
输出
以下是上述代码的输出。
Pride and Prejudice
1813
Jane Austen
Elizabeth Bennet
使用values()方法
使用values()方法来遍历字典元素的值。
返回字典中每个项目的所有值的可迭代对象。然后,可以使用for循环按照下面所示的方式遍历这些值。
示例
这种方法防止了访问keys()方法,这通常是不必要的。因此,使用这种方式遍历字典是最快的方法。
Python字典提供的items()方法允许您循环遍历字典条目。
以下是使用values()方法遍历字典的示例:
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for values in dictionary.values():
print(values)
输出
下面是上述代码的输出结果:
Pride and Prejudice
1813
Jane Austen
Elizabeth Bennet
使用索引进行迭代
可以使用项的索引来迭代字典。在不使用keys()、values()或items()方法的情况下迭代字典类似于这样。
示例
以下是一个使用索引迭代字典的示例 –
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for index in dictionary:
print(index, dictionary[index])
输出
以下是上述代码的输出。
Novel Pride and Prejudice
year 1813
author Jane Austen
character Elizabeth Bennet
按字母顺序迭代字典
通常,字典不保持任何排序。这意味着迭代项的顺序不能保证。Python中的sorted()函数可以用于按给定顺序迭代字典。首先对项进行排序,然后使用for循环遍历它。
示例
sorted(dictionary.keys()) 对字典的键进行排序, for 循环遍历sorted函数返回的键。
以下是通过索引迭代字典的示例-
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for keys in sorted(dictionary.keys()):
print(keys, dictionary[keys])
输出
以下是上述代码的输出。
Novel Pride and Prejudice
author Jane Austen
character Elizabeth Bennet
year 1813
使用字典项值排序
在对字典按照其值进行排序之前,您必须先生成一个已排序的键集。
排序后的键集可以进行迭代,每次迭代都可以使用键访问字典。
示例
使用 sorted keys = sorted(dictionary,key=dictionary.get) 生成排序后的键集。
在排序后的键集中, values: - 重复排列的键集
sorted_dictionary[values] = dictionary[values] 访问字典并添加值到排序后的字典中。现在,排序后的值将存在于最终的字典中。为了验证结果,打印它。
下面是一个按照字典项值排序的示例 −
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
sorted_dictionary = {}
sorted_key = sorted(dictionary, key=dictionary.get)
for values in sorted_key:
sorted_dictionary[values] = dictionary[values]
print(sorted_dictionary)
输出
以下是上述代码的输出结果:
{'year': '1813', 'character': 'Elizabeth Bennet', 'author': 'Jane Austen', 'Novel': 'Pride and Prejudice'}