简体   繁体   English

Python:生成列表的所有有序组合

[英]Python: Generating all ordered combinations of a list

I'm using Python 2.7. 我正在使用Python 2.7。

I'm having a list, and I want all possible ordered combinations. 我有一个列表,我想要所有可能的有序组合。

import itertools
stuff = ["a","b","c", "d"]
for L in range(1, len(stuff)+1):
    for subset in itertools.combinations(stuff, L):
        print( ' '.join(subset))

This will give the following output: 这将给出以下输出:

a
b
c
d
a b
a c <-- not in correct order
a d <-- not in correct order
b c
b d <-- not in correct order
c d
a b c
a b d <-- not in correct order
a c d <-- not in correct order
b c d
a b c d

But I want the output only to be combinations that are in the same order as the stuff list. 但我希望输出只是与stuff列表顺序相同的组合。 Eg removing ad , bd , abd and acd since these are not in correct order compared to the stuff list ["a", "b", "c", "d"] . 例如,删除adbdabdacd因为与stuff列表["a", "b", "c", "d"]相比,这些顺序不正确。

I've figured out using this instead: 我已经想出了使用它:

import itertools
stuff = ["a","b","c", "d"]
for L in range(1, len(stuff)+1):
    for subset in itertools.combinations(stuff, L):
        if ' '.join(subset) in ' '.join(stuff): #added line
            print( ' '.join(subset))

Is giving me the output I wanted: 给我我想要的输出:

a
b
c
d
a b
b c
c d
a b c
b c d
a b c d

But is there any built-in method in Python that does what I want? 但是,Python中是否有任何内置方法可以满足我的需求?

I believe what you are looking for are all possible slices of your original list. 我相信你所寻找的是原始列表的所有可能片段 Your desired output translated into slices is this: 您想要的输出转换成切片是这样的:

a         # slices[0:1]
b         # slices[1:2]
c         # slices[2:3]
d         # slices[3:4]
a b       # slices[0:2]
b c       # slices[1:3]
c d       # slices[2:4]
a b c     # slices[0:3]
b c d     # slices[1:4]
a b c d   # slices[0:4]

So what you should try to produce are those indexes. 所以你应该尝试生成的是那些索引。 And if you look closely and sort them, you can see that those are the 2-combinations of numbers between 0 and 4, where the first number is smaller than the other—which is exactly what itertools.combinations does for a list of indexes. 如果仔细观察并对它们进行排序,您可以看到它们是0到4之间的数字的2组合,其中第一个数字小于另一个数字 - 这正是itertools.combinations对索引列表的作用。 So we can just generate those: 所以我们可以生成这些:

for i, j in itertools.combinations(range(len(stuff) + 1), 2):
    print(stuff[i:j])

This produces the following output: 这会产生以下输出:

['a']
['a', 'b']
['a', 'b', 'c']
['a', 'b', 'c', 'd']
['b']
['b', 'c']
['b', 'c', 'd']
['c']
['c', 'd']
['d']

The advantage is that this produces actual sublists of your input, and doesn't care if those where single characters in the first place. 优点是,这会生成输入的实际子列表,并不关心首先是单个字符的那些。 It can be any kind of content in a list. 它可以是列表中的任何类型的内容。

If the output order is of any importance, you can order by the output list size to get the desired result: 如果输出顺序具有任何重要性,您可以按输出列表大小排序以获得所需的结果:

def getCombinations (lst):
    for i, j in itertools.combinations(range(len(lst) + 1), 2):
        yield lst[i:j]

for x in sorted(getCombinations(stuff), key=len):
    print(' '.join(x))

I think you mean in continuous order by "in correct order", in this case you just need use two pointers to iterator over stuff : 我认为你的意思是按照“正确的顺序”连续顺序,在这种情况下你只需要使用两个指针来迭代stuff

stuff = ["a","b","c", "d"]
# sort stuff here if it's not sorted

result = []
for i in xrange(len(stuff)):
    for j in xrange(i+1, len(stuff)+1):
        result.append(stuff[i:j])

# sort the result by length, maybe you don't need it
result = sorted(result, key=len)

for r in result:
    print ' '.join(r)

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

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