[英]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.