2016-06-28 48 views
-1

我有號碼的清單:如何從列表中找到所有可能的升序和降序?

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

請[編輯]您quesiton,包括你已經嘗試的代碼。它工作嗎? – 2016-06-28 07:36:21

+0

@Lutz Horn我so6ry。 我已編輯它。 –

+0

我不明白你的預期輸出的後半部分。 '[19,18,17,16,15,14]'也是嗎? – 2016-06-28 07:47:18

回答

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]] 
+0

這是好事。謝謝。 –

相關問題