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]
?
你可以模拟数学模拟:
list1 = [1, 2, 3, 4]
print(list1[4 % len(list1)])
Run Code Online (Sandbox Code Playgroud)
1
Run Code Online (Sandbox Code Playgroud)