Python 打印字典中所有键值对的和

Python 打印字典中所有键值对的和

在Python中, dictionary 是一个更常见地被称为关联数组的数据结构的实现。字典由一组键值对组成。每个键值组合对应一个键和其对应的值。

在本文中,我们将学习一个Python程序来打印字典中所有键值对的和。

使用的方法

以下是实现此任务的各种方法:

  • 使用For循环和append()函数(字典遍历方法)

  • 使用dict.keys()函数

  • 使用For循环、keys()函数和values()函数

示例

假设我们输入了一个 输入字典 。现在,我们将使用上述方法打印输入字典的每个项的键和值对的和。

输入

inputDict = {5: 3, 10: 2, 6: 4, 12:8}

输出

8 12 10 20

在上面的输入字典中,每个项目的键和值的总和如下: 第1个项目 − 5+3 = 8 第2个项目 − 10+2=12 第3个项目 − 6+4=10 第4个项目 − 12+8 = 20 因此我们得到的输出为 8 12 10 20

使用for循环和append()函数

在这种方法中,我们将使用简单的for循环和append函数的组合来打印字典中所有键值对的总和。

步骤

以下是执行所需任务的算法/步骤:

  1. 创建一个名为 getKeyValuesSum() 的函数,通过接受输入字典作为参数来打印字典的键和值对之和。
  2. 初始化一个 空列表 ,用于存储每个字典项的键和值之和。
  3. 使用 for循环 遍历输入字典的每个项目。
  4. 将字典的键和值相加,并将其附加到结果列表中。
  5. 遍历结果并打印键值对之和。
  6. 创建一个变量来存储 输入字典
  7. 通过传递输入字典作为参数调用上述定义的 getKeyValuesSum() 函数,然后打印它。

示例

以下程序使用for循环和append()函数返回了输入字典的每个项目的键值对之和:

# creating a function that prints the sum of key and value pairs
# of a dictionary by accepting input dictionary as an argument
def getKeyValuesSum(inputDict):
    # Creating a list to store sum of keys and values of each dictionary item
    resultantList = []
    # traversing through each item of the input dictionary
    for item in inputDict:
        # Appending the key(item) as well as the value(inputDict[item]) to the result
        resultantList.append(item + inputDict[item])
    print("The Sum of key and value pairs of the dictionary is:")
    # Printing the resultant list
    for i in resultantList:
        print(i,end=' ')
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
# Printing input dictionary
print("The Given Dictionary:", inputDict)
# calling the above defined getKeyValuesSum() function
# by passing input dictionary as an argument
getKeyValuesSum(inputDict)

输出

在执行上述程序时,将生成以下输出 –

The Given Dictionary: {5: 3, 10: 2, 6: 4, 12: 8}
The Sum of key and value pairs of the dictionary is:
8 12 10 20

使用dict.keys()函数

在这种方法中,我们将使用key()函数和dict.keys()方法,该方法提供了一个视图对象,按插入顺序显示字典中所有键的列表。

示例

以下程序使用keys()函数返回输入字典的每个项目的键和值对的求和结果:

def getKeyValuesSum(inputDict):
    # storing the sum of keys and values of each dictionary item
    resultantList = []
    # traversing through the keys of the input dictionary
    for k in inputDict.keys():
        # Appending the key as well as the value(inputDict[key]) to the result
        resultantList.append(k + inputDict[k])
    # Print the values of the resultant list
    print(resultantList)
# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
getKeyValuesSum(inputDict)

输出

执行上述程序后,将生成以下输出:

[8, 12, 10, 20]

使用For循环、keys()和values()函数

在这种方法中,我们将使用keys()和values()函数来打印Python字典中所有键值对的和。

语法

keys() function:

dict.keys()方法提供了一个视图对象,按插入的顺序显示字典中的所有键的列表。

values() function:

dict.values() 方法提供了一个视图对象,按插入顺序显示字典中所有值的列表。

步骤

下面是执行所需任务的算法/步骤:

  • 创建一个变量来存储输入字典。

  • 使用 dict.keys() 函数获取输入字典的所有键的列表。

  • 使用 dict.values() 函数获取输入字典的所有值的列表。

  • 初始化一个空列表来存储每个字典项的键和值的和。

  • 使用 for 循环遍历范围为键列表的长度(使用 len() 函数返回对象中的项目数)。

  • 获取当前元素在键和值列表中的和,并使用 append() 函数将其追加到结果列表中。

  • 打印结果列表。

示例

以下程序使用 for 循环、keys() 和 values() 函数返回输入字典每个项的键和值对的和。

# input dictionary
inputDict = {5: 3, 10: 2, 6: 4, 12: 8}
# getting a list of keys of an input dictionary
keysList = list(inputDict.keys())
# getting a list of values of an input dictionary
valuesList = list(inputDict.values())
# storing the sum of keys and values of each dictionary item in a list
resultantList = []
# traversing in a range till the length of the keys list
for p in range(0, len(keysList)):
  # appending the sum of current elements in both keys and value list and
    resultantList.append(keysList[p]+valuesList[p])
for e in resultantList:
  # printing current element
    print(e, end=" ")

输出

在执行时,上述程序将生成以下输出 –

8 12 10 20

结论

在本文中,我们学习了如何使用3种不同的方法打印字典中所有键值对的和。我们还学习了如何利用keys()和values()函数分别获取所有键和字典值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程