python - 在 Python 上对不同深度的列表子列表进行排列,同时保持它们独立

标签 python python-3.x list iterator combinations

我需要对不同深度的子列表进行排列,并同时在 Python 上将它们分开。 例如:

Input: x = [[a,b],[d],[e, [g,h]]

Output:  
[['a', 'b'], ['d'], ['e', ['g', 'h']]]
[['a', 'b'], ['d'], [['g', 'h'], 'e']]
[['b', 'a'], ['d'], ['e', ['g', 'h']]]
[['b', 'a'], ['d'], [['g', 'h'], 'e']]
[['a', 'b'], ['d'], ['e', ['h', 'g']]]
[['a', 'b'], ['d'], [['h', 'g'], 'e']]
[['b', 'a'], ['d'], ['e', ['h', 'g']]]

所以我需要迭代子列表中的子列表中的所有子列表等等......

我尝试了这个,但它迭代第一级的子列表并且不会排列 ['g','h'] 部分:

def product_perms(x):
    perms = [list(map(list,permutations(i))) for i in x]
    for x in product(*perms):
        print(list(x))

非常感谢。

最佳答案

您可以使用带有生成器的递归:

from collections import Counter
x = [['a','b','c'],['d'],['e','f',['g','h','i']]] 
def can_add(a, b):
    '''checks that the frequency of types in b is <= frequency of types in a'''
    c, d = Counter(map(type, a)), Counter(map(type, b))
    return all(c[k] >= j for k, j in d.items()) and all(isinstance(i, list) or i in a for i in b)

def combo(d, c = []):
   '''main recursive procedure'''
   def _combo(new_d, c = []):
      '''find combinations for individual list'''
      if len(c) == 2:
         yield c
      else:
         for i in filter(lambda x:x not in c, new_d):
            if isinstance(i, list):
               for k in combo(i):
                  if can_add(new_d, c+[k]):
                     yield from _combo(new_d, c+[k])
            else:
                if can_add(new_d, c+[i]):
                  yield from _combo(new_d, c+[i])
   if all(isinstance(i, list) for i in d):
      if len(d) == 1:
         if len(d[0]) == 1:
            yield d
         else:
            yield from map(lambda x:[x], _combo(d[0]))
      else:
         _r = list(combo(d[1:]))
         for i in ([d[0]] if len(d[0]) == 1 else _combo(d[0])):
           if _r:
             for b in _r:
               yield [i, *b]
           else:
              yield i
   elif d:
      yield from _combo(d)
<小时/>
_, *result = combo(x)

示例输出:

[[['a', 'b'], ['d'], ['e', ['g', 'h']]], 
 [['a', 'b'], ['d'], ['e', ['g', 'i']]], 
 [['a', 'b'], ['d'], ['e', ['h', 'g']]], 
 [['a', 'b'], ['d'], ['e', ['h', 'i']]], 
 [['a', 'b'], ['d'], ['e', ['i', 'g']]], 
 [['a', 'b'], ['d'], ['e', ['i', 'h']]], 
 ....

关于python - 在 Python 上对不同深度的列表子列表进行排列,同时保持它们独立,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58232710/

相关文章:

python - 如何将值添加到 Pandas 数据框中的新列?

python-3.x - 如何使用 AWS Lambda 在 AWS Sagemaker 中远程启动 Jupyter Notebook

r - 如何在 R 中的 2 个列表的元素之间应用函数?

java - 关于 AbstractCollection 实现

python - 分割和剥离输出 python

python - 如何使用 openpyxl 查找包含图像的单元格

python - 如何解析 PubMed 文本文件?

python - 在 Julia 中,如何像在 Python 中那样重复一个字符 n 次来创建一个字符串?

python - 在没有ctypes的情况下修改bdb中的变量值

python - 从 Python 2D 列表中删除列的最佳/快速方法