嵌套列表中的置换元组组合

时间:2018-11-07 02:22:37

标签: python list tuples

我有一个嵌套列表,想在列表中排列组合。

test = [[('a',)],
        [('b', 'c'), ('d', 'e')],
        [('f', 'g'), ('h', 'i'), ('j', 'k')],
        [('l', 'm'), ('n', 'o'), ('p', 'q')]]

我的预期输出是这样的:

[(('a',), ('b', 'c')),
 (('a',), ('d', 'e')),
 (('a',), ('f', 'g')),
 (('a',), ('h', 'i')),
 (('a',), ('j', 'k')),
 (('b', 'c'), ('f', 'g')),
 (('b', 'c'), ('h', 'i')),
 (('b', 'c'), ('j', 'k')),
 (('d', 'e'), ('f', 'g')),
 (('d', 'e'), ('h', 'i')),
 (('d', 'e'), ('j', 'k')),
 (('a',), ('b', 'c'), ('f', 'g')),
 (('a',), ('b', 'c'), ('h', 'i')),
 (('a',), ('b', 'c'), ('j', 'k')),
 (('a',), ('d', 'e'), ('f', 'g')),...,
 (('a',), ('b', 'c'), ('f', 'g'), ('l', 'm')), ...]

进一步详细说明,我的最终目标是在元组列表中从2的排列到乘积的排列进行置换,其逻辑与没有自置换的逻辑相同。即,如果嵌套列表中有5个子列表,则我将从2到5的组合中进行置换。类似[((),()),...,((),(),()),...,((),(),(),()),...,((),(),(),(),()),...]

我尝试过list(itertools.combinations(itertools.chain(*test),2)),但是我不想在子列表之间进行排列。例如,我要排除

((('b', 'c'), ('d', 'e')),
 (('f', 'g'), ('h', 'i')),
 (('f', 'g'), ('j', 'k')),
 (('h', 'i'), ('j', 'k')),
 (('f', 'g'), ('h', 'i'), ('j', 'k')),...)

1 个答案:

答案 0 :(得分:1)

您可以使用递归:

test = [[('a',)],
    [('b', 'c'), ('d', 'e')],
    [('f', 'g'), ('h', 'i'), ('j', 'k')]] 

def _product(d):
   def combinations(d, _c = []):
      for i, a in enumerate(d):
        for c in a:
          if len(_c) == 1 and not any(all(t in h for t in _c+[c]) for h in d):
            yield tuple(sorted(_c+[c]))
          yield from combinations(d[1:], _c = [] if len(_c) > 0 else _c+[c])
   r = list(combinations(d))
   return [a for i, a in enumerate(r) if a not in r[:i]]

print(_product(test))

输出:

[(('a',), ('b', 'c')), 
 (('a',), ('d', 'e')), 
 (('a',), ('f', 'g')), 
 (('a',), ('h', 'i')), 
 (('a',), ('j', 'k')), 
 (('b', 'c'), ('f', 'g')), 
 (('b', 'c'), ('h', 'i')), 
 (('b', 'c'), ('j', 'k')), 
 (('d', 'e'), ('f', 'g')), 
 (('d', 'e'), ('h', 'i')), 
 (('d', 'e'), ('j', 'k'))]

修改

要查找所有排列,请创建一种方法以找到一定长度的排列,然后在enter输入的范围内进行迭代,并对完整结果使用列表推导:

def product(d, _len):
  def combinations(d, _d, current):
    if len(current) == _d:
      yield tuple(sorted(current))
    else:
      if d:
        for i in d:
          for c in i:
            _c = current+[c]
            if not current or (not any(all(t in h for t in _c) for h in d) and len(set(_c))) == len(_c):
              yield from combinations(d, _d, _c)
  r = list(combinations(d, _len, []))
  return [a for i, a in enumerate(r) if a not in r[:i]]

def full_product(test):
  return [i for b in range(2, len(test)+1) for i in product(test, b)]

for i in full_product(test):
  print(i)

输出:

(('a',), ('b', 'c'))
(('a',), ('d', 'e'))
(('a',), ('f', 'g'))
(('a',), ('h', 'i'))
(('a',), ('j', 'k'))
(('b', 'c'), ('f', 'g'))
(('b', 'c'), ('h', 'i'))
(('b', 'c'), ('j', 'k'))
(('d', 'e'), ('f', 'g'))
(('d', 'e'), ('h', 'i'))
(('d', 'e'), ('j', 'k'))
(('a',), ('b', 'c'), ('d', 'e'))
(('a',), ('b', 'c'), ('f', 'g'))
(('a',), ('b', 'c'), ('h', 'i'))
(('a',), ('b', 'c'), ('j', 'k'))
(('a',), ('d', 'e'), ('f', 'g'))
(('a',), ('d', 'e'), ('h', 'i'))
(('a',), ('d', 'e'), ('j', 'k'))
(('a',), ('f', 'g'), ('h', 'i'))
(('a',), ('f', 'g'), ('j', 'k'))
(('a',), ('h', 'i'), ('j', 'k'))
(('b', 'c'), ('d', 'e'), ('f', 'g'))
(('b', 'c'), ('f', 'g'), ('h', 'i'))
(('b', 'c'), ('f', 'g'), ('j', 'k'))
(('b', 'c'), ('d', 'e'), ('h', 'i'))
(('b', 'c'), ('h', 'i'), ('j', 'k'))
(('b', 'c'), ('d', 'e'), ('j', 'k'))
(('d', 'e'), ('f', 'g'), ('h', 'i'))
(('d', 'e'), ('f', 'g'), ('j', 'k'))
(('d', 'e'), ('h', 'i'), ('j', 'k'))

编辑2:在更新的full_product变量上运行test时,长度为4时输出的一部分为:

...
(('a',), ('b', 'c'), ('d', 'e'), ('f', 'g'))
(('a',), ('b', 'c'), ('d', 'e'), ('h', 'i'))
(('a',), ('b', 'c'), ('d', 'e'), ('j', 'k'))
(('a',), ('b', 'c'), ('d', 'e'), ('l', 'm'))
(('a',), ('b', 'c'), ('d', 'e'), ('n', 'o'))
(('a',), ('b', 'c'), ('d', 'e'), ('p', 'q'))
...