有没有办法循环索引

Adi*_*tya 8 python indexing list cycle python-3.x

list1 = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

如果我list1如上所示,最后一个值的索引是3,但有没有办法,如果我说list1[4],它会成为list1[0]

Ste*_*uch 7

你可以模拟数学模拟:

码:

list1 = [1, 2, 3, 4]
print(list1[4 % len(list1)])
Run Code Online (Sandbox Code Playgroud)

结果:

1
Run Code Online (Sandbox Code Playgroud)