Python,订单和列表
问题描述:
我需要帮助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],我想我的订单中有相应的编号。 找出这个,我会拯救我的一天!谢谢:)
答
我认为你有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
尝试'shuffle'进行就地洗牌的列表 – tuned