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


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

在这篇文章中,我们将学习一个Python程序,用于打印字典中所有键值对的和。

使用的方法

以下是完成此任务的各种方法

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

  • 使用dict.keys()函数

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

示例

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

输入

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

输出

8 12 10 20 

在上述输入字典中,每个项目的键和值的和为:

第一项 − 5+3 = 8

第二项 − 10+2=12

第三项 − 6+4=10

第四项 − 12+8 = 20

因此,我们得到输出为8 12 10 20

使用For循环和append()函数

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

算法(步骤)

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

  • 创建一个函数getKeyValuesSum(),通过接受输入字典作为参数来打印字典的键值对的和。

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

  • 使用for循环遍历输入字典的每个项。

  • 将字典的键和值相加,并将其添加到结果列表中。

  • 遍历结果并打印键值对的和。

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

  • 通过将输入字典作为参数调用上面定义的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()函数

在这种方法中,我们将使用keys()函数,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 

结论

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

更新于:2023年8月18日

444 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.