簡體   English   中英

如何從列表中獲取“子”列表的所有組合

[英]How to get all combinations of “sub” lists from a list

因此,如果我從以下內容開始:

x = [a, b, c]
y = [[a], [b], [c], [a,b], [b,c]]

如何從y元素得到x所有組合? 就像是:

y = [ [[a, b], c], [[b,c], a], [a, b, c]

我研究過itertools和列表理解,但仍然很努力。 原始列表中的所有元素都必須出現在結果列表的每個項目中。

如何獲得列表中每個項目的組合?

遍歷列表,然后在該循​​環內再次遍歷列表:

x = [a, b, c]
y = []
for i in x:
    for j in x:
        y.append([i, j])

請注意,您要求的確切輸出不是“列表中每個項目的組合”-實際上,您的確切輸出似乎並不一致,這意味着標准庫中的任何內容或任何內容都無法幫助您,但這應該給您一個起點。

您可以將遞歸函數與生成器一起使用:

def full_length(d):
  return sum(1 if not isinstance(i, list) else full_length(i) for i in d)

def semi_combos(d, current=[]):
   if len(current) == len(x) - 1:
     yield current
   else:
     for i in d:
       if len(set(current+[i])) == len(current)+1:
         yield from semi_combos(d, current+[i])

def remainder_combos(d, _target, current = []):
  if full_length(current)+len(_target) == len(x):
    yield [_target, *current]
  for i in d:
    if i not in _target and i not in current:
      yield from remainder_combos(d, _target, current+[i])


for x in [['a', 'b', 'c'], ['a', 'b', 'c', 'd']]:
  print([b for i in semi_combos(x) for b in list(remainder_combos(x, i))])

輸出:

[[['a', 'b'], 'c'], [['a', 'c'], 'b'], [['b', 'a'], 'c'], [['b', 'c'], 'a'], [['c', 'a'], 'b'], [['c', 'b'], 'a']]

[[['a', 'b', 'c'], 'd'], [['a', 'b', 'd'], 'c'], [['a', 'c', 'b'], 'd'], [['a', 'c', 'd'], 'b'], [['a', 'd', 'b'], 'c'], [['a', 'd', 'c'], 'b'], [['b', 'a', 'c'], 'd'], [['b', 'a', 'd'], 'c'], [['b', 'c', 'a'], 'd'], [['b', 'c', 'd'], 'a'], [['b', 'd', 'a'], 'c'], [['b', 'd', 'c'], 'a'], [['c', 'a', 'b'], 'd'], [['c', 'a', 'd'], 'b'], [['c', 'b', 'a'], 'd'], [['c', 'b', 'd'], 'a'], [['c', 'd', 'a'], 'b'], [['c', 'd', 'b'], 'a'], [['d', 'a', 'b'], 'c'], [['d', 'a', 'c'], 'b'], [['d', 'b', 'a'], 'c'], [['d', 'b', 'c'], 'a'], [['d', 'c', 'a'], 'b'], [['d', 'c', 'b'], 'a']]

現在,這是假定您具有可以使用的有效組合的列表。 使用遞歸函數,我能夠獲得所需的輸出。

注意:我已經做了0次優化

import numpy


def build_combo(current_set, all_combos):
    global count, req_options
    for each in all_combos:
        if all(len(numpy.intersect1d(each, x)) == 0 for x in current_set):
            inner_set = current_set.copy()
            inner_set.append(each)
            flat = []
            for x in inner_set:
                flat.extend(x)
            if all(x in flat for x in req_options):
                built_combos.append(inner_set)
            else:
                build_combo(inner_set, all_combos)


req_options = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

all_combos = [['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['g'],
              ['a', 'b'], ['c', 'd'], ['a', 'f'], ['f', 'g'],
              ['a', 'c', 'd'], ['g', 'f', 'e', 'b']]

built_combos = []
build_combo([], all_combos)

option_sets = set()
for combo in built_combos:
    newset = set()
    for element in combo:
        newset.add(frozenset(element))
    option_sets.add(frozenset(newset))

for option_set in option_sets:
    combo_str = ''
    for option in option_set:
        combo_str += '[{}]'.format('+'.join(option))
    print(combo_str)

輸出(在打印輸出上添加了“ +”號,以便於閱讀):

[e][g][c+d][b][f+a]
[a][c][d][f+e+g+b]
[f+g][e][c][d][a+b]
[f+g][e][c+d][a][b]
[f][e][c][g][a][b][d]
[c+a+d][f][e][g][b]
[b][e][f+g][c+a+d]
[f][e][g][c+d][a+b]
[f+g][e][a+b][c+d]
[f][e][g][c+d][a][b]
[e][c][g][b][f+a][d]
[f+g][e][c][a][b][d]
[f][e][c][g][d][a+b]
[a][f+e+g+b][c+d]
[c+a+d][f+e+g+b]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM