Python 最小值键分配

Python 最小值键分配

Python中值键分配的意思是在字典中将一个值与特定键关联起来,这样可以基于键值对进行高效的数据检索和操作。它可以为各种目的创建和组织结构化数据结构。

Python对数据结构的实现更常见地被称为关联数组,具体实现是 字典 。(结构没有给翻译) 一个字典由一组键值对组成。每个键值组合对应一个键和其对应的值。

示例

假设我们有两个 输入字典 ,其中的键相同。我们将比较两个字典中每个键的值,并将其中的最小值分配给该键,并使用上述方法打印出结果字典。

输入

inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3}
inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}

输出

Resultant dictionary after assigning minimum value keys:
 {'hello': 2, 'tutorialspoint': 5, 'users': 3}

在上述两个字典中,2小于6,因此我们将 hello 键分配为最小值2。 同样,对于 tutorialspoint 键,10大于5,因此5为最小值。 对于 users 键,3小于7,因此3为最小值。 使用的方法如下: - 使用字典推导、min()和items()函数 - 使用dict()、min()和zip()函数

方法1:使用字典推导、min()和items()函数

min() 函数(返回可迭代对象中的最小值) items() 函数(返回一个视图对象,即包含字典的键值对的元组列表)。

minimum_value = min(iterable)
items_list = dictionary.items()

步骤

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

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

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

  • 打印输入的字典。

  • 使用items()函数遍历第一个字典。

  • 检查第一个字典的最小值键和第二个字典的值(由于键是共同的,我们可以直接使用[]操作符得到它)。

  • 使用字典推导式来存储具有最小值的共同键。

  • 在分配最小值键后打印结果字典。

示例

以下程序使用字典推导式、min()和items()函数,在分配最小值键后返回一个字典。

# input dictionary 1
inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3}
# input dictionary 2
inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}
# printing input dictionaries
print("Input dictionary 1: ", inputDict_1)
print("Input dictionary 2: ", inputDict_2)
# Getting the minimum value of the keys from the first and second dictionaries.
# Assigning them with the common key with the dictionary comprehension
resultantDict = {k: min(v, inputDict_2[k]) for k, v in inputDict_1.items()}
# printing resultant dictionary
print("Resultant dictionary after assigning minimum value keys:\n", resultantDict)

输出

Input dictionary 1:  {'hello': 2, 'tutorialspoint': 10, 'users': 3}
Input dictionary 2:  {'hello': 6, 'tutorialspoint': 5, 'users': 7}
Resultant dictionary after assigning minimum value keys:
 {'hello': 2, 'tutorialspoint': 5, 'users': 3}

方法2:使用dict()、min()和zip()函数

dict() 函数(将对象转换为字典)

函数 zip() 可用于组合两个列表/迭代器。

zip(*iterables)

步骤

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

  • 使用zip函数以组合方式迭代两个字典。

  • 使用min()函数找到键的最小值。

  • 在分配最小值键后打印结果字典。

示例

以下程序使用dict()、min()和zip()函数分配最小值键后返回一个字典。

# input dictionary 1
inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3}
# input dictionary 2
inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}
# printing input dictionaries
print("Input dictionary 1: ", inputDict_1)
print("Input dictionary 2: ", inputDict_2)
# Iterate in both dictionaries by passing them in the zip() function.
# Get the minimum value of these both dictionaries using the min() function
resultantDict = dict([min(i, j) for i, j in zip(
    inputDict_1.items(), inputDict_2.items())])
# printing resultant dictionary
print("Resultant dictionary after assigning minimum value keys:\n", resultantDict)

输出

Input dictionary 1:  {'hello': 2, 'tutorialspoint': 10, 'users': 3}
Input dictionary 2:  {'hello': 6, 'tutorialspoint': 5, 'users': 7}
Resultant dictionary after assigning minimum value keys:
 {'hello': 2, 'tutorialspoint': 5, 'users': 3}

结论

在本文中,我们学习了两种不同的最小值键分配方法。此外,我们还学习了如何使用zip函数来组合两个字典或可迭代对象的迭代。最后,我们还学习了如何应用一些字典操作,并使用字典解析在字典中进行迭代。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程