【问题标题】:Unique permutations of list from 2 to N in PythonPython中列表从2到N的唯一排列
【发布时间】:2020-03-08 16:27:31
【问题描述】:

我正在尝试在循环中生成一个排列列表,并为每次迭代打印输出超过两个(或写入文件的行)。

示例输入列表:

['one', 'two', 'three', 'four']

需要的输出:

['one', 'two', 'three', 'four']
['two', 'three', 'four']
['one', 'three', 'four']
['one', 'two', 'four']
['one', 'two']
['one', 'three']
['one', 'four']
['two', 'three']
['two', 'four']
['three', 'four']

这是我迄今为止所做的(在我的 Python 生命的早期,请原谅):

from itertools import permutations

input = ['one', 'two', 'three', 'four']

def convertTuple(tup): 
    str =  ''.join(tup) 
    return str

while (len(input) > 1):    
    permlist = set(permutations(input))
    for i in permlist:
        print(i)
        i = convertTuple(i)
        outfile = open("out.txt", "w")
        outfile.write(i)
    input = input[:-1]
else:
    print("End of permutation cycle")

哪些输出:

('two', 'three', 'one', 'four')
('two', 'four', 'one', 'three')
('three', 'two', 'one', 'four')
('four', 'two', 'one', 'three')
('two', 'one', 'three', 'four')
('two', 'one', 'four', 'three')
('three', 'one', 'four', 'two')
('four', 'one', 'three', 'two')
('one', 'two', 'three', 'four')
('one', 'two', 'four', 'three')
('three', 'four', 'one', 'two')
('four', 'three', 'one', 'two')
('two', 'three', 'four', 'one')
('two', 'four', 'three', 'one')
('three', 'two', 'four', 'one')
('three', 'four', 'two', 'one')
('four', 'two', 'three', 'one')
('four', 'three', 'two', 'one')
('three', 'one', 'two', 'four')
('four', 'one', 'two', 'three')
('one', 'four', 'two', 'three')
('one', 'three', 'two', 'four')
('one', 'three', 'four', 'two')
('one', 'four', 'three', 'two')
('two', 'three', 'one')
('three', 'two', 'one')
('three', 'one', 'two')
('one', 'two', 'three')
('one', 'three', 'two')
('two', 'one', 'three')
('two', 'one')
('one', 'two')
End of permutation cycle

我知道我错了

input = input[:-1]

因为它只是删除了原始列表中的最后一个值,但我不知道如何只获取每个列表中具有不同数量值的唯一列表...

我是否使用了 itertools 的错误部分?我应该使用组合还是其他方式?

我被严重卡住了,所以非常感谢任何帮助!

谢谢!

【问题讨论】:

    标签: python loops combinations permutation itertools


    【解决方案1】:

    我不知道如何仅获取唯一列表,每个列表中的值数量不同...我应该使用组合还是其他方式?

    是的,使用combinations 以避免以不同的顺序获得相同的集合。

    我知道input = input[:-1] 有问题

    对;不要自己从候选集中删除项目。你想要的功能已经内置了。

    >>> import itertools
    >>> help(itertools.combinations)
    Help on class combinations in module itertools:
    
    class combinations(builtins.object)
     |  combinations(iterable, r) --> combinations object
     |
     |  Return successive r-length combinations of elements in the iterable.
     |
     |  combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
    

    您需要做的就是提供第二个参数(它也适用于permutations)。无需重复删除元素并查找组合,只需迭代输出大小的可能值即可。

    (顺便说一句,in the "recipes" section of the documentation 有一个巧妙的例子:标记为powerset 的那个。集合中的power set 基本上就是你正在计算的东西,除了还包括所有一个-元素结果和空结果。)

    【讨论】:

      【解决方案2】:

      假设 ('one','two','three') 是您想要的列表中的意外遗漏,这应该可以解决问题:

      from itertools import combinations
      
      l = ['one', 'two', 'three', 'four']
      
      for size in range(2,len(l)+1):
          for i in combinations(l,size):
              print(i)
      

      【讨论】:

        猜你喜欢
        • 2021-11-23
        • 1970-01-01
        • 2019-03-26
        • 2023-02-19
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多