Python程序:字典旋转K位
Python 中实现的一种数据结构,通常称为关联数组,就是字典。字典由一组键值对组成。每个键值组合对应一个键及其对应的值。
给定一个包含一些随机键值对的字典,在本文中,我们将学习如何在 Python 中将给定的字典旋转 K 位。
使用的方法
以下是完成此任务的各种方法
使用列表推导式、items() 和字典推导式
使用字典推导式、deque.rotate() 和 items() 函数
示例
假设我们已经获取了一个**输入字典和 K 次旋转**。我们现在将输入字典旋转**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() 方法为可迭代对象添加一个计数器,并返回 enumerate 对象。
参数
iterable - 可以是任何支持迭代的序列/对象/可迭代对象
start - enumerate() 从此值开始计数。如果未指定 start,则使用值 0。
算法(步骤)
以下是执行所需任务的算法/步骤。
创建一个变量来存储**输入字典**。
打印输入字典。
创建另一个变量来存储**输入的 K 次旋转数**。
使用**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。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP