Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python组合和产品_Python_List_Combinations_Product_Itertools - Fatal编程技术网

Python组合和产品

Python组合和产品,python,list,combinations,product,itertools,Python,List,Combinations,Product,Itertools,我有一个dataframes列列表: 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]

我有一个dataframes列列表:

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的组合。有人能看到我能做的好编辑吗

总结,;我需要名单上所有名字的组合。使用一条规则,您不能有多个以A开头的变量和多个以B开头的变量来表示

all_others=[CD,CF,CG]  #All, 1, 2 or none of these
把它分解为

all_others=[CD]  #one or none of these
all_others=[CF]  #one or none of these
all_others=[CG]  #one or none of these
然后你的代码就变成了

from itertools import product

for 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 product

for 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']
的乘积与
[None]
结合,生成一个迭代器,其中包含

('CD', None), ('CF', None), ('CG', None)

这些正是我们希望传递给
product
的参数
*
操作符将迭代器中的元素转换为函数参数。因此,上述两个代码段是等效的。

请尝试以下操作,而不是
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(选项中的列对应于列,如果列))
的选项输出为:

AA BB
AA BB CD
AA BB CF
AA BB CD CF
AA BC
AA BC CD
AA BC CF
AA BC CD CF
AA
AA CD
AA CF
AA CD CF
AS BB
AS BB CD
AS BB CF
AS BB CD CF
AS BC
AS BC CD
AS BC CF
AS BC CD CF
AS
AS CD
AS CF
AS CD CF
AD BB
AD BB CD
AD BB CF
AD BB CD CF
AD BC
AD BC CD
AD BC CF
AD BC CD CF
AD
AD CD
AD CF
AD CD CF
BB
BB CD
BB CF
BB CD CF
BC
BC CD
BC CF
BC CD CF

CD
CF
CD CF

我建议您将
None
替换为
'
或将其删除。

以下是一种更稳健/通用的方法来完成您想要的事情。我首先定义一个助手函数:

from itertools import combinations, chain, product

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

很难理解你的输出应该是什么。嘿,应该是列表中所有名称的组合。根据一条规则,不能有多个以A开头的变量和多个以B开头的变量。这更有意义吗?谢谢,有更有效的方法吗?这个变量列表可能会变得非常大,如果我想更改B的条件,我不确定是否可以全部键入,也就是说,我必须至少有1b存在,但可以有更多。我该怎么做?是的!!!这就是我想要的那种东西。不错。谢谢你的欢迎,如果你觉得这是正确的,请考虑投票并接受答案。thakns@fred.schwartz我在那里添加了一些更新。看看。谢谢你,本,非常好。我将尝试消化/应用您的代码,并让您知道它是否有效。看起来很有希望。干杯,非常有效。最后一个问题给我留下了深刻的印象,我可以写长度为[CD','CF','CG',[0:3]的子集吗?很高兴它成功了!我想我看到了你想要使用选择器的目的。它似乎不适合这里,因为我们选择的长度只是整数的范围。如果你真的想要,你可以写
range(100)[0:3]
。但这相当于
范围(3)
,它给出了
[0,1,2]
。请注意,由于Python扭曲的索引约定,我们在末尾缺少
3
,因此我们需要
范围(4)
。这就是为什么对于小列表,我更喜欢显式地写出它们。