我有一个数字列表:
number = [1,2,3,4,5,6,...,20]
我想从我的列表中生成所有可能的升序和降序6。
possible_ordered:
[1,2,3,4,5,6]
[2,3,4,5,6,7]
[3,4,5,6,7,8]
...
[15,16,17,18,19,20]
[20,19,18,17,16,15]
[15,14,13,12,11,10]
[10,9,8,7,6,5]
[6,5,4,3,2,1]
这是我到目前为止所尝试的。
a = [1,2,3,4,5,6,7,8,9,10]
for x in a:
b = a[a.index(x):a.index(x)+6]
if len(b) == 6:
print b
输出:
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 10]
我不知道如何获得降序,而且我不确定这是一个好的代码。
答案 0 :(得分:0)
试试这个:
a = list(range(1, 21))
b = list(range(20, 0, -1)) # the revers of a
print([a[start: start + 6] for start in range(len(a) - 5)] +
[b[start: start + 6] for start in range(len(a) - 5)])
输出:
[[1, 2, 3, 4, 5, 6],
[2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 8],
[4, 5, 6, 7, 8, 9],
[5, 6, 7, 8, 9, 10],
[6, 7, 8, 9, 10, 11],
[7, 8, 9, 10, 11, 12],
[8, 9, 10, 11, 12, 13],
[9, 10, 11, 12, 13, 14],
[10, 11, 12, 13, 14, 15],
[11, 12, 13, 14, 15, 16],
[12, 13, 14, 15, 16, 17],
[13, 14, 15, 16, 17, 18],
[14, 15, 16, 17, 18, 19],
[15, 16, 17, 18, 19, 20],
[20, 19, 18, 17, 16, 15],
[19, 18, 17, 16, 15, 14],
[18, 17, 16, 15, 14, 13],
[17, 16, 15, 14, 13, 12],
[16, 15, 14, 13, 12, 11],
[15, 14, 13, 12, 11, 10],
[14, 13, 12, 11, 10, 9],
[13, 12, 11, 10, 9, 8],
[12, 11, 10, 9, 8, 7],
[11, 10, 9, 8, 7, 6],
[10, 9, 8, 7, 6, 5],
[9, 8, 7, 6, 5, 4],
[8, 7, 6, 5, 4, 3],
[7, 6, 5, 4, 3, 2],
[6, 5, 4, 3, 2, 1]]