Python程序:将列表向右旋转n

创新互联Python教程:

编写一个 Python 程序,将列表向右旋转 n 次或N个位置。在这个 Python 右旋转列表示例中,我们使用负数从右侧对列表进行切片,并使用算术+进行组合。

rtList = [10, 20, 30, 40, 50, 60, 70]

rtRotate = int(input("Enter Position to Rotate List Items = "))

print('Original List Items Before Right Rotating')
print(rtList)

list1 = rtList[-rtRotate:] + rtList[:-rtRotate]

print('\nFinal List Items After Right Rotating')
print(list1)

在这个 Python 程序中,我们使用嵌套的 for 循环范围,根据用户给定的数字 n 或位置向右旋转列表项。

rtList = [10, 20, 30, 40, 50, 60, 70]

rtRotate = int(input("Enter Position to Rotate List Items = "))

print('Original List Items Before Right Rotating')
print(rtList)

length = len(rtList) - 1

for i in range(rtRotate):
    lastValue = rtList[length]
    for j in range(length,-1, -1):
        rtList[j] = rtList[j - 1]
    rtList[0] = lastValue

print('Final List Items After Right Rotating')
print(rtList)
Enter Position to Rotate List Items = 5
Original List Items Before Right Rotating
[10, 20, 30, 40, 50, 60, 70]
Final List Items After Right Rotating
[30, 40, 50, 60, 70, 10, 20]

该 Python 程序允许输入列表大小、项目、位置,并将列表项目向右侧旋转给定的位置。

def printListItems(rtList):
    for i in range(len(rtList)):
        print(rtList[i], end = '   ')

rtList = []
rtListTot = int(input("Total Number of List Items = "))

for i in range(1, rtListTot + 1):
    rtListvalue = int(input("Please enter the %d List Item = "  %i))
    rtList.append(rtListvalue)

rtRotate = int(input("Enter Position to Rotate List Items = "))

print('\nOriginal List Items Before Right Rotating')
printListItems(rtList)

length = len(rtList) - 1

for i in range(rtRotate):
    lastValue = rtList[length]
    for j in range(length,-1, -1):
        rtList[j] = rtList[j - 1]
    rtList[0] = lastValue

print('\nFinal List Items After Right Rotating')
printListItems(rtList)
Total Number of List Items = 9
Please enter the 1 List Item = 2
Please enter the 2 List Item = 4
Please enter the 3 List Item = 6
Please enter the 4 List Item = 8
Please enter the 5 List Item = 10
Please enter the 6 List Item = 12
Please enter the 7 List Item = 14
Please enter the 8 List Item = 16
Please enter the 9 List Item = 18
Enter Position to Rotate List Items = 3

Original List Items Before Right Rotating
2   4   6   8   10   12   14   16   18   
Final List Items After Right Rotating
14   16   18   2   4   6   8   10   12 

Python 程序使用 while 循环向右旋转列表 n 次

def printListItems(rtList):
    i = 0
    while i < len(rtList):
        print(rtList[i], end = '   ')
        i = i + 1

rtList = []
rtListTot = int(input("Total Number of List Items = "))
i = 1
while i <= rtListTot:
    rtListvalue = int(input("Please enter the %d List Item = "  %i))
    rtList.append(rtListvalue)
    i = i + 1

rtRotate = int(input("Enter Position to Rotate List Items = "))

print('\nOriginal List Items Before Right Rotating')
printListItems(rtList)

length = len(rtList) - 1
i = 0
while i < rtRotate:
    lastValue = rtList[length]
    j = length
    while j >= 0:
        rtList[j] = rtList[j - 1]
        j = j - 1
    i = i + 1
    rtList[0] = lastValue

print('\nFinal List Items After Right Rotating')
printListItems(rtList)
Total Number of List Items = 7
Please enter the 1 List Item = 22
Please enter the 2 List Item = 44
Please enter the 3 List Item = 66
Please enter the 4 List Item = 88
Please enter the 5 List Item = 128
Please enter the 6 List Item = 148
Please enter the 7 List Item = 168
Enter Position to Rotate List Items = 2

Original List Items Before Right Rotating
22   44   66   88   128   148   168   
Final List Items After Right Rotating
148   168   22   44   66   88   128 

当前名称:Python程序:将列表向右旋转n
转载来源:http://www.shufengxianlan.com/qtweb/news34/360134.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联