亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 組合和產品

Python 組合和產品

一只斗牛犬 2021-09-11 20:29:43
我有一個數據框列的列表:L=[AA ,  AS  ,  AD  , BB  , BC  , CD ,  CF,CG ]我需要所有項目的組合,沒有特定的順序。但是,在每個組合中我只能有一個以 A 開頭的名字,但我可以有多個以 C 開頭或沒有開頭的名字。關于 B,我必須至少有 1 個 B,但可以有更多所以我需要所有的組合A=[AA,AS,AD] #only one of theseB=[BB,BC]  #at least one of theseall_others=[CD,CF,CG]  #All, 1, 2 or none of these到目前為止,我有這個代碼;from itertools import productfor 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 開頭的變量
查看完整描述

3 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

試試這個而不是你的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 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用''或刪除它們。


查看完整回答
反對 回復 2021-09-11
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

當然要表達


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']with的乘積會[None]產生一個迭代器,其中包含


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

這些正是我們希望傳遞給product 的*參數。運算符將迭代器內的元素轉換為函數參數。因此上面的兩個代碼片段是等價的。


查看完整回答
反對 回復 2021-09-11
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

這是做你想要的那種事情的更強大/更通用的方法。我首先定義一個輔助函數:


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))


查看完整回答
反對 回復 2021-09-11
  • 3 回答
  • 0 關注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號