简体   繁体   English

Python数字列表排列出现意外结果

[英]Python number list permutations with unexpected results

I am attempting to take a list of numbers 0-9 inclusive and return all the permutations of the list. 我正在尝试获取包含0-9的数字的列表,并返回列表的所有排列。 I have come up with two different functions that return the expected result to a certain extent but, neither is exactly what I am aiming for. 我提出了两个不同的函数,它们在一定程度上返回了预期的结果,但它们都不是我的目标。 Here is one that returns the correct results for one cycle: 这是一个可以在一个周期内返回正确结果的结果:

x = [0,1,2,3,4,5,6,7,8,9]

def test(x):
  place_holder = 9
  count = 9
  print x
  while count > 1:
    old_x = x[count]
    x[count] = x[count-1]
    x[count-1] = old_x
    count -= 1
    print x
    if count == 1:
      x.sort()
      place_holder -= 1
      count = place_holder

Returns: 返回值:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
[0, 1, 2, 3, 4, 5, 6, 9, 7, 8]
[0, 1, 2, 3, 4, 5, 9, 6, 7, 8]
[0, 1, 2, 3, 4, 9, 5, 6, 7, 8]
[0, 1, 2, 3, 9, 4, 5, 6, 7, 8]
[0, 1, 2, 9, 3, 4, 5, 6, 7, 8]
[0, 1, 9, 2, 3, 4, 5, 6, 7, 8]
[0, 9, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
[0, 1, 2, 3, 4, 5, 8, 6, 7, 9]
[0, 1, 2, 3, 4, 8, 5, 6, 7, 9]
[0, 1, 2, 3, 8, 4, 5, 6, 7, 9]
[0, 1, 2, 8, 3, 4, 5, 6, 7, 9]
[0, 1, 8, 2, 3, 4, 5, 6, 7, 9]
[0, 8, 1, 2, 3, 4, 5, 6, 7, 9]
[0, 1, 2, 3, 4, 5, 7, 6, 8, 9]
[0, 1, 2, 3, 4, 7, 5, 6, 8, 9]
[0, 1, 2, 3, 7, 4, 5, 6, 8, 9]
[0, 1, 2, 7, 3, 4, 5, 6, 8, 9]
[0, 1, 7, 2, 3, 4, 5, 6, 8, 9]
[0, 7, 1, 2, 3, 4, 5, 6, 8, 9]
[0, 1, 2, 3, 4, 6, 5, 7, 8, 9]
[0, 1, 2, 3, 6, 4, 5, 7, 8, 9]
[0, 1, 2, 6, 3, 4, 5, 7, 8, 9]
[0, 1, 6, 2, 3, 4, 5, 7, 8, 9]
[0, 6, 1, 2, 3, 4, 5, 7, 8, 9]
[0, 1, 2, 3, 5, 4, 6, 7, 8, 9]
[0, 1, 2, 5, 3, 4, 6, 7, 8, 9]
[0, 1, 5, 2, 3, 4, 6, 7, 8, 9]
[0, 5, 1, 2, 3, 4, 6, 7, 8, 9]
[0, 1, 2, 4, 3, 5, 6, 7, 8, 9]
[0, 1, 4, 2, 3, 5, 6, 7, 8, 9]
[0, 4, 1, 2, 3, 5, 6, 7, 8, 9]
[0, 1, 3, 2, 4, 5, 6, 7, 8, 9]
[0, 3, 1, 2, 4, 5, 6, 7, 8, 9]
[0, 2, 1, 3, 4, 5, 6, 7, 8, 9]

Though when I use another list in the permutation, it gives unexpected results: 尽管当我在置换中使用另一个列表时,它会产生意外的结果:

x = [1,0,2,3,4,5,6,7,8,9]

[1, 0, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 0, 2, 3, 4, 5, 6, 7, 9, 8]
[1, 0, 2, 3, 4, 5, 6, 9, 7, 8]
[1, 0, 2, 3, 4, 5, 9, 6, 7, 8]
[1, 0, 2, 3, 4, 9, 5, 6, 7, 8]
[1, 0, 2, 3, 9, 4, 5, 6, 7, 8]
[1, 0, 2, 9, 3, 4, 5, 6, 7, 8]
[1, 0, 9, 2, 3, 4, 5, 6, 7, 8]
[1, 9, 0, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
[0, 1, 2, 3, 4, 5, 8, 6, 7, 9]
[0, 1, 2, 3, 4, 8, 5, 6, 7, 9]
[0, 1, 2, 3, 8, 4, 5, 6, 7, 9]
[0, 1, 2, 8, 3, 4, 5, 6, 7, 9]
[0, 1, 8, 2, 3, 4, 5, 6, 7, 9]
[0, 8, 1, 2, 3, 4, 5, 6, 7, 9]
[0, 1, 2, 3, 4, 5, 7, 6, 8, 9]
[0, 1, 2, 3, 4, 7, 5, 6, 8, 9]
[0, 1, 2, 3, 7, 4, 5, 6, 8, 9]
[0, 1, 2, 7, 3, 4, 5, 6, 8, 9]
[0, 1, 7, 2, 3, 4, 5, 6, 8, 9]
[0, 7, 1, 2, 3, 4, 5, 6, 8, 9]
[0, 1, 2, 3, 4, 6, 5, 7, 8, 9]
[0, 1, 2, 3, 6, 4, 5, 7, 8, 9]
[0, 1, 2, 6, 3, 4, 5, 7, 8, 9]
[0, 1, 6, 2, 3, 4, 5, 7, 8, 9]
[0, 6, 1, 2, 3, 4, 5, 7, 8, 9]
[0, 1, 2, 3, 5, 4, 6, 7, 8, 9]
[0, 1, 2, 5, 3, 4, 6, 7, 8, 9]
[0, 1, 5, 2, 3, 4, 6, 7, 8, 9]
[0, 5, 1, 2, 3, 4, 6, 7, 8, 9]
[0, 1, 2, 4, 3, 5, 6, 7, 8, 9]
[0, 1, 4, 2, 3, 5, 6, 7, 8, 9]
[0, 4, 1, 2, 3, 5, 6, 7, 8, 9]
[0, 1, 3, 2, 4, 5, 6, 7, 8, 9]
[0, 3, 1, 2, 4, 5, 6, 7, 8, 9]
[0, 2, 1, 3, 4, 5, 6, 7, 8, 9]

Where it goes through the nine cycle properly then goes back to 0-9. 它正确经过九个周期的位置,然后返回到0-9。 So I could see this is because of the x.sort() call. 所以我可以看到这是由于x.sort()调用。 So I changed this function to this: 因此,我将此功能更改为:

def exp_test(x):
  static = []
  for i in x:
    static.append(i)
  place_holder = 9
  count = 9
  print x
  while count > 1:
    old_x = x[count]
    x[count] = x[count-1]
    x[count-1] = old_x
    count -= 1
    print x
    if count == 1:
      x = static
      place_holder -= 1
      count = place_holder

Now this works fine until the shift of the seven and it goes to every second number. 现在,此方法可以正常工作,直到七个移位为止,并且移至第二个数字。 I figure the count got mixed up but, I go through and don't see it? 我认为计数混淆了,但是,我经过了,没看到吗?

Try modifying x = static in the last if statement to x = static[:] . 尝试在最后一个if语句中将x = static修改为x = static[:] The problem is that you are simply rebinding the name x to the same list that static is bound to. 问题在于您只是将名称x重新绑定到static绑定到的同一列表。 You really want to make a copy of what static is bound to instead. 您确实想复制static绑定的内容。

The best solution would be 最好的解决方案是

from itertools import permutations

but if you must write it yourself, the usual solution is recursive: 但是,如果必须自己编写,通常的解决方案是递归的:

def permutations(seq):
    _len = len(seq)
    if _len:
        if _len==1:
            yield seq
        else:
            for p in permutations(seq[1:]):
                for i in range(_len):
                    yield p[:i] + seq[0:1] + p[i:]

Edit: well, the Euler problem requires a different approach altogether... the trick is not to generate all permutations up to 1,000,000 (which would be far too slow), but to calculate what the millionth permutation must be. 编辑:好吧,欧拉问题完全需要一种不同的方法...诀窍不是生成所有排列高达1,000,000(这太慢了),而是计算百万分之一的排列。 There are n! 有n! ways to arrange n items - you can recursively use this on the tail of the sequence to figure out how many subsequences have to be rearranged to get to the millionth arrangement, and from that work out what the arrangement must be. 排列n个项目的方法-您可以在序列的末尾递归使用此方法,以找出必须重新排列多少个子序列才能达到百万分之一的排列,并从中得出排列必须是什么。

You need to write something more like 你需要写一些更像

def nth_arrangement(seq, n):
    # you have to figure this bit out!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM