Python:生成列表的所有有序组合 [英] Python: Generating all ordered combinations of a list

查看:822
本文介绍了Python:生成列表的所有有序组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 2.7.

I'm using 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))

这将给出以下输出:

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

但是我只希望输出是与stuff列表相同顺序的组合.例如.删除a db da b da c d,因为与stuff列表["a", "b", "c", "d"]相比,它们的顺序不正确.

But I want the output only to be combinations that are in the same order as the stuff list. E.g. removing a d, b d, a b d and a c d since these are not in correct order compared to the stuff list ["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

但是Python中有任何内置方法可以实现我想要的功能吗?

But is there any built-in method in Python that does what I want?

推荐答案

我相信您正在寻找的是原始列表中所有可能的切片.您想要的输出转换成切片是这样的:

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]

因此,您应该尝试生成的是那些索引.而且,如果仔细观察并进行排序,您会发现它们是0到4之间的数字的2个组合,其中第一个数字小于另一个数字-这正是itertools.combinations对索引列表的作用.这样我们就可以生成这些:

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. So we can just generate those:

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

这将产生以下输出:

['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))

这篇关于Python:生成列表的所有有序组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆