Python 循环列表项的位置
我正在尝试从系统中的用户角色循环 我有一个来自用户ID的列表:Python 循环列表项的位置,python,list,Python,List,我正在尝试从系统中的用户角色循环 我有一个来自用户ID的列表: users = [12, 13, 14, 15] 我想做一个函数,每次调用它时,我都可以更改列表的顺序: #function() users = [13, 14, 15, 12] #function() users = [14, 15, 12, 13] #function() users = [15, 12, 13, 14] 我查过谷歌,但没有找到一个好方法。我在pop中考虑第一个值,并在每次创建函数时重新生成列表,并将pop项
users = [12, 13, 14, 15]
我想做一个函数,每次调用它时,我都可以更改列表的顺序:
#function()
users = [13, 14, 15, 12]
#function()
users = [14, 15, 12, 13]
#function()
users = [15, 12, 13, 14]
我查过谷歌,但没有找到一个好方法。我在pop中考虑第一个值,并在每次创建函数时重新生成列表,并将pop项放在最后一个位置。
有没有更像蟒蛇的方法呢?试试这个:
def firsttolast():
global users
toappend = users.pop(0)
users.append(toappend)
您可以这样使用:
users = [12,13,14,15]
firsttolast()
print(users)
整个代码:
def firsttolast():
global users
toappend = users.pop(0)
users.append(toappend)
users = [12,13,14,15]
firsttolast()
print(users)
可以使用切片使其成为圆形。我编写了示例函数,但您可以根据需要修改它
#CLOCKWISE
def changeSeq(x): return x[-1:]+x[:-1]
#ANTICLOCKWISE
def changeSeq(x): return x[1:]+x[0:1]
def changeSeqx:返回x[-1:]+x[:-1]
u=[12,13,14,15]
版图
u=变更顺序u
版图
u=变更顺序u
版图
输出
[12, 13, 14, 15]
[15, 12, 13, 14]
[14, 15, 12, 13]