【发布时间】:2018-07-30 07:42:17
【问题描述】:
好的,所以我想在索引中创建一个循环迭代器,如下所示
[0,1,2,3,4,5,6,7,8][1,2,3,4,5,6,7,8,0][2,3,4,5,6,7,8,0,1][3,4,5,6,7,8,0,1,2]...[8,0,1,2,3,4,5,6,7]
其中最大值可能是 8 或其他数字。
到目前为止,我有一些代码不起作用。
a = ['l','m','n','o','p','q','r','s','t'] #len(a) = 9
b = [[]] *len(a)
c = [[]] *len(a)
for offset_index in range(len(a)):
b[offset_index] = []
c[offset_index] = []
for i in range(offset_index, len(a) - offset_index, 1):
b[offset_index].append(i)
c[offset_index].append(a[i])
print b
[[0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6 ], [3, 4, 5], [4], [], [], [], []]
我可以看出这是第二个范围函数的问题,但无法想象一个巧妙的解决方法。
【问题讨论】:
标签: python python-2.7 loops offset