简体   繁体   English

我如何找到所有可能的方法来组合列表中的项目而无需重复?

[英]How do I find all possible ways to combine items in a list without repetition?

I have six categories: A, B, C, D, E, and F. 我有六个类别:A,B,C,D,E和F。

I want to find out all the unique ways I can combine the categories, without repetition. 我想找出所有我可以组合类别而不重复的独特方法。

For example, if I combine the first three categories, I will get A, A, A, B, C, D. If I combine B, C, D, E, I will get A, B, B, B, B, C. 例如,如果我组合前三个类别,我将得到A,A,A,B,C,D。如果我组合B,C,D,E,我将得到A,B,B,B,B, C。

I tried itertools. 我尝试过itertools。 itertools.product comes close, but there is a lot of repetition. itertools.product接近,但是有很多重复。 For example, I get A, B, A, A, C, D, but I also get B, A, B, B, D, C, which is a duplicate in my case. 例如,我得到了A,B,A,A,C,D,但我也得到了B,A,B,B,D,C,在我的情况下是重复的。 The order matters, replacement matters, the count matter, but the character does not matter. 顺序很重要,更换很重要,数量很重要,但字符无关紧要。

Since you only have 6 categories you can use itertools.product and then filter your result according to your criteria. 由于只有6个类别,因此可以使用itertools.product,然后根据您的条件过滤结果。

Your examples are somewhat confusing as I'm not sure how you get 'AAABCD' from the first three categories 'ABC' which doesn't contain 'D', or how you get 'ABBBBC' by combining 'BCDEI' which doesn't contain 'A'. 您的示例有些令人困惑,因为我不确定如何从前三个类别中不包含“ D”的“ ABC”中获得“ AAABCD”,或者如何通过组合不包含“ D”的“ BCDEI”来获得“ ABBBBC”包含“ A”。 However assuming you want to get all the unique combinations of some subset of 'ABCDEF', of length 6, up to symbol replacement you can do this. 但是,假设您要获取长度为6的'ABCDEF'某些子集的所有唯一组合,直到符号替换,您都可以这样做。

from itertools import product

CATEGORIES = 'ABCDEF'

def combinations(cats):
    # use itertools to get all combinations 
    all_combs = product(cats,repeat=len(CATEGORIES))
    valid_combs = set()

    # For every possible combination find the order in which the characters appear
    for s in all_combs:
        s = ''.join(s)
        order = []
        for c in s:
            if c not in order: 
                order.append(c)

        # replace the character by ones following a set predetermined order
        for i,c in enumerate(order):
            replace_char = CATEGORIES[i].lower()
            s = s.replace(c, replace_char)

        # add to set to remove duplicates
        s = s.upper()
        valid_combs.add(s)
    return list(valid_combs)

usage 用法

combinations('AB') 
['ABABBB', 'ABABBA', 'AABBBB', 'ABAABB', 'ABBAAA', 'AABAAA', 'AABABB', 'AAABAB', 'AABABA', 'AABAAB', 'ABAAAB', 'AABBAB', 'AAAAAB', 'ABBAAB', 'ABBABA', 'ABBABB', 'AAAABA', 'ABAAAA', 'AAABAA', 'ABAABA', 'ABBBAB', 'AAABBB', 'ABBBBA', 'AAABBA', 'AABBAA', 'ABABAA', 'AAAAAA', 'ABBBBB', 'ABABAB', 'ABBBAA', 'AABBBA', 'AAAABB']

The rationale of this is that if 'ABAACD' and 'BABBDC' belong to the same equivalence class, then the member where the characters appear in order is a unique representative of that equivalence class. 其基本原理是,如果“ ABAACD”和“ BABBDC”属于同一个等效类,则字符按顺序出现的成员是该等效类的唯一代表。

This isn't very efficient though, so for a much larger list of categories you may need to construct the list directly. 但是,这并不是很有效,因此对于更大的类别列表,您可能需要直接构造列表。

暂无
暂无

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

相关问题 找出列表元素的所有可能方式的差异 - Find the differences in all possible ways of list elements 如何在每次迭代中成对组合列表元素而不重复? - How can I combine elements of list in pairs in every iteration without repetition? 如何找到数字重复的唯一性? - How do I find the uniqueness of the repetition of numbers? 我如何找到所有可能的方法,我可以将一个数组安装到 4 个插槽中,并且数组可能有更多/少于 4 个数字? - How do I find all possible ways I can fit an array to 4 slots with the possibility of the array having more/less than 4 numbers? 如何以所有可能的方式将列表拆分成对 - How to split a list into pairs in all possible ways 如何有效地生成所有可能的坐标对而不在numpy中重复 - How to generate all possible pairs of coordinates without repetition in numpy efficiently 如何从python中的列表中获取所有可能的组合以允许重复 - How to get all possible combinations from a list in python allowing repetition 如何找到所有可能的方法将字符串分成 n 片而不递归? - How to find all possible ways to divide a string into n slices without recursion? 从根到叶的所有可能方式的列表(无二叉树) - List of all possible ways from root to leaf (without binary tree) 在不使用itertools的情况下,以所有可能的方式组合两个字符到Python中的特定长度 - Combine two characters in all possible ways without itertools to a certain length in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM