Python 以K为单位旋转字典
Python对于一种被更常见地称为关联数组的数据结构的实现是字典。字典由一组键值对构成。每个键值对应于一个键和其相应的值。
给定一个带有一些随机键值对的字典,在本文中,我们将学习如何在Python中将给定的字典按K进行旋转。
使用的方法
以下是完成此任务的各种方法:
- 使用列表解析、items()和字典解析
-
使用字典解析、deque.rotate()和items()函数
示例
假设我们已经取得一个 输入字典和K次旋转 。我们现在将按照 k次 旋转输入字典,并打印旋转后的结果字典。
输入
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
k=3
输出
{1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
在上面的例子中,K值为3。输入字典后:
第一次旋转:{5:1, 2: 5, 4: 6, 1: 3, 9: 4}
第二次旋转:{9:4, 5:1, 2: 5, 4: 6, 1: 3}
第三次旋转:{1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
方法1 使用列表推导,items() 和字典推导
在这种方法中,我们将了解如何使用列表推导和字典推导来将字典旋转K次。
语法
enumerate(iterable, start=0)
enumerate()方法会为可迭代对象添加一个计数器,并返回一个枚举对象。
参数
- iterable- 可以是任何支持迭代的序列/对象/可迭代对象
-
start- enumerate()从这个值开始计数。如果未指定start,则使用值0。
步骤
下面是执行所需任务的算法/步骤:
- 创建一个变量用于存储。
-
打印输入的字典。
-
创建另一个变量用于存储。
-
使用 items() 函数(返回一个视图对象,即一个包含字典的键值对的列表中的元组)获取字典的键和值。
-
使用 list() 函数将其转换为元组的列表(将序列/可迭代对象转换为列表)
-
使用enumerate()函数在列表的索引、元素上进行遍历,并使用数学逻辑将其旋转k次。
-
再次使用字典推导将元组的结果列表转换回字典。
-
在字典旋转k次后,打印结果字典。
示例
以下程序使用列表推导、items()和字典推导返回一个通过给定的输入K次旋转输入字典后的字典:
# input dictionary
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
# printing input dictionary
print("Input Dictionary:", inputDict)
# input K rotations
k = 3
# getting the key, and value of the dictionary as a tuple and converting it into the list
inputDict = list(inputDict.items())
# rotationg the input dictionary by k rotations
resultList = [inputDict[(p - k) % len(inputDict)] for p, m in enumerate(inputDict)]
# converting the result list of tuples into a dictionary again
# using dictionary comprehension
resultantDict = {e[0]: e[1] for e in resultList}
# printing the resultant dictionary after given input k rotations
print("Resultant dictionary after", k, "rotations:", resultantDict)
输出
执行上述程序后,将生成以下输出:
Input Dictionary: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
Resultant dictionary after 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
方法2 使用字典推导、deque.rotate()和items()函数
双向队列,也称为deque,允许用户从任一端添加和删除项目。Deque模块属于collections库。它提供了可以直接使用参数进行添加和删除项目的方法。
当我们需要从容器的两端进行更快的附加和弹出操作时,选择deque而不是列表,因为deque提供了O(1)的附加和弹出操作的时间复杂度,而列表提供了O(n)的时间复杂度。
deque.rotate()函数 - 使用此函数按照给定参数中的数字旋转deque。如果给定的数字是负数,旋转向左发生。否则,旋转将向右发生。
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字从collections模块导入deque。
-
使用items()函数获取字典的键和值对。
-
使用list()函数将其转换为元组列表(将序列/可迭代对象转换为列表)。
-
通过将输入的字典作为参数传递给deque()函数,将字典转换为deque。
-
通过将输入的k值传递给rotate()函数,并将其应用于deque,使输入的字典旋转k次。
-
使用list()函数(返回可迭代对象的列表)将结果转换为列表。
-
使用字典推导将结果的元组列表再次转换为字典。
-
打印旋转字典k次后的结果字典。
示例
以下程序使用字典推导、deque.rotate()和items()函数,通过给定输入的k次旋转,返回一个经过旋转的字典:
# importing deque from the collections module
from collections import deque
# input dictionary
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
# printing input dictionary
print("Input Dictionary:", inputDict)
# input K rotations
k = 3
# getting the key, value of dictionary as a tuple
# and converting it into the list
inputDict = list(inputDict.items())
# Converting input dictionary to deque
dequeObject = deque(inputDict)
# rotating the deque object by k rotations
dequeObject.rotate(k)
# converting into the list
resultList = list(dequeObject)
# converting the result list of tuples into a dictionary again
resultantDict = {e[0]: e[1] for e in resultList}
# printing the resultant dictionary after input k rotations
print("Resultant dictionary after", k, "rotations:", resultantDict)
输出
执行以上程序后,将会生成以下输出:
Input Dictionary: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
Resultant dictionary after 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
结论
在本文中,我们学习了两种不同的方法来将字典旋转K次。我们还学习了如何使用rotate()函数将旋转添加到一个deque对象,并且如何将给定的字典转换为deque。