【发布时间】:2021-12-31 08:26:09
【问题描述】:
我想在python中按范围交换元素列表
我的代码是
List = [1,2,3,4,5,6]
def swap(list_, a, b):
list_[a], list_[b] = list_[b], list_[a]
swap(List, 0, 5,)
print(List)
我的输出是
[6, 2, 3, 4, 5, 1]
但我想要的是列表索引范围交换
# my expected output
n = 2 (add var by input)
#and it swapped to this
[3, 4, 5, 6, 1, 2]
【问题讨论】:
标签: python-3.x list