Python,订单和列表

时间:2017-10-15 15:14:50

标签: python-3.x list

我需要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,4,3,5]它会去上。小心你必须使用每个字母一次才能形成一个列表。

基本上我的程序给了我一个列表,如[1,5,0,3,4,2],我希望在我的订单中有相应的编号。 找到这个,我会节省我的一天!谢谢:))

1 个答案:

答案 0 :(得分: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