2017-10-15 117 views
1

我需要幫助python。 (請原諒我的英語) 我有一個列表的順序:從[0,1,2,3,4,5]到[5,4,3,2,1,0](這是一種字母以「字母」0,1,2,3,4和5順序排列)。例如[0,1,2,3,4,5]後面的[0,1,2,3,5,4]是[0,1,2,3,5,4],然後是[0,1,2,4,3,5]上。小心你必須使用每個字母一次形成一個列表。Python,訂單和列表

基本上我的程序給了我一個清單,如[1,5,0,3,4,2],我想我的訂單中有相應的編號。 找出這個,我會拯救我的一天!謝謝:)

+0

嘗試'shuffle'進行就地洗牌的列表 – tuned

回答

0

我認爲你有permutations,並且需要index

>>> from itertools import permutations 
>>> L = list(permutations(range(6),6)) # Generate the list 
>>> len(L) 
720 
>>> L[0]   # First item 
(0, 1, 2, 3, 4, 5) 
>>> L[-1]   # Last item 
(5, 4, 3, 2, 1, 0) 
>>> L.index((0,1,2,3,4,5)) # Find a sequence 
0 
>>> L.index((5,4,3,2,1,0)) 
719 
>>> L.index((1,5,0,3,4,2)) 
219 

注意所產生的名單是元組,而不是列出的名單列表。如果你想列表清單:

>>> L = [list(x) for x in permutations(range(6),6)] 
>>> L.index([1,5,0,3,4,2]) 
219