Python 组合和产品

我有一个数据框列的列表:


L=[AA ,  AS  ,  AD  , BB  , BC  , CD ,  CF,CG ]

我需要所有项目的组合,没有特定的顺序。


但是,在每个组合中我只能有一个以 A 开头的名字,但我可以有多个以 C 开头或没有开头的名字。


关于 B,我必须至少有 1 个 B,但可以有更多


所以我需要所有的组合


A=[AA,AS,AD] #only one of these

B=[BB,BC]  #at least one of these

all_others=[CD,CF,CG]  #All, 1, 2 or none of these

到目前为止,我有这个代码;


from itertools import product


for choices in product(['AA','AS','AD',None],['BB', 'BC', None], ['CD','CF', None],):

    print(' '.join(column for column in choices if column))

然而,这有效,它只允许一个以 C 开头的值,但我想要 C 的product所有组合。任何人都可以看到我可以进行的良好编辑吗?


总结; 我需要列表中名称的所有组合。根据一条规则,您不能有多个以 A 开头的变量和多个以 B 开头的变量


一只斗牛犬
浏览 196回答 3
3回答

达令说

试试这个而不是你的for循环:for choices in itertools.product(['AA','AS','AD',None],['BB', 'BC', None],[' '.join(k) for j in list(itertools.combinations(['CD','CF'],i) for i in range(3)) for k in j]):    # do what you need使用选项的输出print(' '.join(column for column in choices if column))是:AA BBAA BB CDAA BB CFAA BB CD CFAA BCAA BC CDAA BC CFAA BC CD CFAAAA CDAA CFAA CD CFAS BBAS BB CDAS BB CFAS BB CD CFAS BCAS BC CDAS BC CFAS BC CD CFASAS CDAS CFAS CD CFAD BBAD BB CDAD BB CFAD BB CD CFAD BCAD BC CDAD BC CFAD BC CD CFADAD CDAD CFAD CD CFBBBB CDBB CFBB CD CFBCBC CDBC CFBC CD CFCDCFCD CF我建议你更换None用''或删除它们。

aluckdog

当然要表达all_others=[CD,CF,CG]  #All, 1, 2 or none of these把它分解为all_others=[CD]  #one or none of theseall_others=[CF]  #one or none of theseall_others=[CG]  #one or none of these然后你的代码变成from itertools import productfor choices in product(['AA','AS','AD',None],['BB', 'BC', None], ['CD', None], ['CF', None], ['CG', None],):    print(' '.join(column for column in choices if column))这处理这个特殊的例子。但是,如果您有多个以 C 开头的项目,则可以按如下方式更系统地处理它们:from itertools import productfor choices in product(['AA','AS','AD',None],['BB', 'BC', None], *product(['CD', 'CF', 'CG'], [None]),):    print(' '.join(column for column in choices if column))为了解释发生了什么,取['CD', 'CF', 'CG']with的乘积会[None]产生一个迭代器,其中包含('CD', None), ('CF', None), ('CG', None)这些正是我们希望传递给product 的*参数。运算符将迭代器内的元素转换为函数参数。因此上面的两个代码片段是等价的。

牧羊人nacy

这是做你想要的那种事情的更强大/更通用的方法。我首先定义一个辅助函数:from itertools import combinations, chain, productdef subsets_of_length(s, lengths):    return chain.from_iterable(combinations(s,l) for l in lengths)它产生以下输出:>>>> list(subsets_of_length(['a','b','c'], range(2,4)))[('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]>>>> list(subsets_of_length(['d','e'], range(0,2)))[(), ('d',), ('e',)]现在我们要组合两个或多个子集如下>>>> for choices in product(         subsets_of_length(['a','b','c'], range(2,4)),         subsets_of_length(['d','e'], range(0,2)),     ):         print(' '.join(str(subset) for subset in choices))('a', 'b') ()('a', 'b') ('d',)('a', 'b') ('e',)('a', 'c') ()('a', 'c') ('d',)('a', 'c') ('e',)('b', 'c') ()('b', 'c') ('d',)('b', 'c') ('e',)('a', 'b', 'c') ()('a', 'b', 'c') ('d',)('a', 'b', 'c') ('e',)但是我们想将这些元组链接在一起。因此我们应该这样做>>>> for choices in map(chain.from_iterable,product(         subsets_of_length(['a','b','c'], range(2,4)),         subsets_of_length(['d','e'], range(0,2)),     )):         print(' '.join(column for column in choices if column))a ba b da b ea ca c da c eb cb c db c ea b ca b c da b c e您编辑的问题案例的代码是:for choices in map(chain.from_iterable,product(    subsets_of_length(['AA','AS','AD'], [1]),       #only one of these    subsets_of_length(['BB','BC'], [1,2]),          #at least one of these    subsets_of_length(['CD','CF','CG'], [0,1,2,3]), #All, 1, 2 or none of these)):    print(' '.join(column for column in choices if column))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python