简体   繁体   English

查找N组中N个项目的所有组合,而不重复项目组合(python)?

[英]Find all combinations of N items in N groups without duplicates of item combos (python)?

I'm trying to figure out a way of calculating all possible combinations of features of a certain product and have them returned as a list, without duplicates. 我试图找出一种计算某种产品功能的所有可能组合的方法,并将它们作为列表返回,没有重复。

I have the items (features of the product) grouped in this kind of manner: 我有以这种方式分组的项目(产品的功能):

a
   a1
   a2
   a3

b
   b1
   b2

c
   c1
   c2
   c3
   c4

The number of groups and items are unknown, so there are in fact N groups and N items. 组和项目的数量是未知的,因此实际上有N组和N项。

Example combinations: 示例组合:

# Combinations with 3 groups
a1_b1_c1
a1_b2_c1
a1_b3_c1
...and so on

# Combinations with 4 groups
a1_b1_c1_d1
a1_b2_c1_d1
a1_b3_c1_d1
...and so on

I would consider a1_b2_c3 and a1_c3_b2 to be duplicates, and I would not want any duplicates in the returned list. 我会认为a1_b2_c3a1_c3_b2是重复的,我不希望在返回的列表中有任何重复。

A product without all features, such as a1_b2 or b2 would not be a valid product and thus I do not want those in the returned list either. 没有所有功能的产品(例如a1_b2b2将不是有效产品,因此我不希望返回列表中的那些产品。

I've looked into itertools but I'm stuck. 我看过itertools,但我被卡住了。 Any ideas? 有任何想法吗?

You definitely want itertools.product: 你肯定想要itertools.product:

import itertools

for i in itertools.product(['a1','a2','a3'], ['b1','b2'],['c1','c2','c3','c4']):
    print '_'.join(i)

RETURNS 退货

a1_b1_c1
a1_b1_c2
a1_b1_c3
...
a3_b2_c1
a3_b2_c2
a3_b2_c3
a3_b2_c4

暂无
暂无

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

相关问题 如何找到从第一个列表中选择一个项目和从第二个列表中选择最多 n 个项目的所有组合 - How to find all combinations of picking one item from the first list and at most n items from the second list 在python中生成两项的所有可能长度为n的组合 - Generating all possibly length n combinations of two items in python Python:创建N的组合,其中2个项目为常数 - Python: create combinations of N where 2 of the items are constant n个长度为l的n个项目的所有组合,其中n &lt;l - All combinations of n items with lenght l, where n < l 找出大小至少为 k 到 n 的所有组合 - Find all combinations of at least size k to n 从 N 个桶中生成所有项目组合的算法,其中每个桶只能有 1 个项目 - Algorithm to make all combinations of items from N buckets where only 1 item can come from each bucket 将 N 个项目的所有组合生成到两个袋子中,其中每个项目在一个或零个袋子中 - Generate all combinations of N items into two bags, whereby each item is in one or zero bags C# 将一个列表拆分为 n 个组的所有组合 - 从 Python 的代码迁移 - C# split a list into all combinations of n groups - code migration from Python 从不重复的元组列表中获取具有相同 N 个交集的所有元组组的最快算法 - Fastest algorithm to get all the tuple groups that has the same N intersections from a list of tuples without duplicates 如何在python中获取长度为n的所有组合 - How to get all combinations of length n in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM