简体   繁体   English

获取python中列表的所有排列而没有重复?

[英]Get all permutations of a list in python without duplicates?

I am trying to write a script that gets a set of strings-我正在尝试编写一个获取一组字符串的脚本-

["ab", "ls", "u"]

Then creates every possible combination of them, but doesn't necessarily use all of them.然后创建它们的所有可能组合,但不一定使用所有它们。 I want possible outputs for the above example to be:我希望上述示例的可能输出为:


ab
ab ls
ab ls u
ab u ls
ab u

ls
ls ab
ls ab u
ls u ab
ls u

u
u ls
u ls ab
u ab ls
u ab

My script, having removed the other things it does:我的脚本删除了它所做的其他事情:

stuff = ["ab", "ls", "u"]

for subset in itertools.permutations(stuff):
    concat = ""
    for part in subset:
        concat = concat + part

    #the rest of my script now uses this data

It returns:它返回:

ablsu
abuls
lsabu
lsuab
uabls
ulsab

How would I make it return what I want?我如何让它返回我想要的?

You can use combinations and permutations together.您可以一起使用组合和排列。 This should be able to get you going这应该能让你继续前进

a = ["ab", "ls", "u"]
for i in range(1, len(a)+1):
    for comb in combinations(a, i):
        for perm in permutations(comb):
            print(perm)

Output:输出:

('ab',)
('ls',)
('u',)
('ab', 'ls')
('ls', 'ab')
('ab', 'u')
('u', 'ab')
('ls', 'u')
('u', 'ls')
('ab', 'ls', 'u')
('ab', 'u', 'ls')
('ls', 'ab', 'u')
('ls', 'u', 'ab')
('u', 'ab', 'ls')
('u', 'ls', 'ab')

You can handle comb how ever you see fit您可以随心所欲地处理comb

As you are giving list with 3 elements permutations is giving you back result with all 3 elements.当您给出包含 3 个元素排列的列表时,会返回包含所有 3 个元素的结果。 You need to supply 1 element to get your ab / ls / u in output.您需要提供 1 个元素才能在输出中获得ab / ls / u You need to supply 2 element to get your ab ls / ab u in output.您需要提供 2 个元素才能在输出中获得ab ls / ab u

So same program you can use by calling it with 1/2 elements in list.因此,您可以通过使用列表中的 1/2 元素调用它来使用相同的程序。

stuff = ["ab", "ls", "u"]

for subset in itertools.permutations(stuff):
    concat = ""
    for part in subset:
        concat = concat + part

    #the rest of my script now uses this data

stuff = ["ab", "ls"]

for subset in itertools.permutations(stuff):
    concat = ""
    for part in subset:
        concat = concat + part


stuff = ["ls", "u"]

for subset in itertools.permutations(stuff):
    concat = ""
    for part in subset:
        concat = concat + part

stuff = ["ab", "ls", "u"]
final_list = []
for subset in itertools.permutations(stuff):
    concat = ""
    for part in subset:
        concat = concat + part
        final_list.append(concat)

print(final_list)

['ab',
 'abls',
 'ablsu',
 'ab',
 'abu',
 'abuls',
 'ls',
 'lsab',
 'lsabu',
 'ls',
 'lsu',
 'lsuab',
 'u',
 'uab',
 'uabls',
 'u',
 'uls',
 'ulsab']
import itertools

stuff = ["ab", "ls", "u"]

for i in range(len(stuff) + 1):
    for x in itertools.permutations(stuff[:i]):
        print(x)

But this solution show all permutations::但是这个解决方案显示了所有排列::

()
('ab',)
('ab', 'ls')
('ls', 'ab')
('ab', 'ls', 'u')
('ab', 'u', 'ls')
('ls', 'ab', 'u')
('ls', 'u', 'ab')
('u', 'ab', 'ls')
('u', 'ls', 'ab')

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

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