Python嵌套列表,所有组合
我有表单的嵌套列表:Python嵌套列表,所有组合,python,combinations,itertools,nested-lists,Python,Combinations,Itertools,Nested Lists,我有表单的嵌套列表: ['A', 'B', ['a0', 'a1', 'a2'], 'C', 'D', ['a0', 'a1', 'a2'], 'E', 'F', ['a0', 'a1', 'a2']] ['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a0']] ['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a1']] ['A', 'B', ['a0'], 'C', 'D', ['a0'],
['A', 'B', ['a0', 'a1', 'a2'], 'C', 'D', ['a0', 'a1', 'a2'], 'E', 'F', ['a0', 'a1', 'a2']]
['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a0']]
['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a1']]
['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a2']]
['A', 'B', ['a0'], 'C', 'D', ['a1'], 'E', 'F', ['a0']]
['A', 'B', ['a0'], 'C', 'D', ['a1'], 'E', 'F', ['a1']]
['A', 'B', ['a0'], 'C', 'D', ['a1'], 'E', 'F', ['a2']]
['A', 'B', ['a0'], 'C', 'D', ['a2'], 'E', 'F', ['a0']]
['A', 'B', ['a0'], 'C', 'D', ['a2'], 'E', 'F', ['a1']]
['A', 'B', ['a0'], 'C', 'D', ['a2'], 'E', 'F', ['a2']]
. . .
. . .
. . .
['A', 'B', ['a2'], 'C', 'D', ['a2'], 'E', 'F', ['a2']]
我想生成关于表单的子列表['a0','a1','a2']的所有组合:
['A', 'B', ['a0', 'a1', 'a2'], 'C', 'D', ['a0', 'a1', 'a2'], 'E', 'F', ['a0', 'a1', 'a2']]
['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a0']]
['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a1']]
['A', 'B', ['a0'], 'C', 'D', ['a0'], 'E', 'F', ['a2']]
['A', 'B', ['a0'], 'C', 'D', ['a1'], 'E', 'F', ['a0']]
['A', 'B', ['a0'], 'C', 'D', ['a1'], 'E', 'F', ['a1']]
['A', 'B', ['a0'], 'C', 'D', ['a1'], 'E', 'F', ['a2']]
['A', 'B', ['a0'], 'C', 'D', ['a2'], 'E', 'F', ['a0']]
['A', 'B', ['a0'], 'C', 'D', ['a2'], 'E', 'F', ['a1']]
['A', 'B', ['a0'], 'C', 'D', ['a2'], 'E', 'F', ['a2']]
. . .
. . .
. . .
['A', 'B', ['a2'], 'C', 'D', ['a2'], 'E', 'F', ['a2']]
共27张名单。我知道我必须使用itertools软件包,但我不知道如何使用。欢迎任何想法。如果只有一层嵌套: 进口itertools def组合列表: 准备一份没有子列表的副本,这样就可以复制更少的副本 复制=[] 林迪斯=[] 对于枚举LST中的i,v: 如果isinstancev,请列出: 如果是一个列表,我们注意索引 附录林迪斯 我们不向副本追加任何内容,因为它无论如何都会被覆盖 copy.appendNone 其他: 复制。附件 ret=[] 我们对所有列表参数进行笛卡尔积 *参数正在分解结构 对于itertools.product*[lst[i]中的项目,用于lindices中的i]: 我们把名单复印一份 curcopy=copy.copy 对于i,拉链中的项目,项目: 我们分配元素 curcopy[i]=项目 我们把副本附在报税表上 复诊 回程网 测试=['A','B',['a0','a1','a2'],'C','D',['b0','b1','b2'],'E','F',['c0','c1','c2']] 印刷体
如果确实希望将返回值中的“a0”包装到数组中,则可以用ret.append[curcopy]替换ret.appendcurcopy。如果只有一层嵌套: 进口itertools def组合列表: 准备一份没有子列表的副本,这样就可以复制更少的副本 复制=[] 林迪斯=[] 对于枚举LST中的i,v: 如果isinstancev,请列出: 如果是一个列表,我们注意索引 附录林迪斯 我们不向副本追加任何内容,因为它无论如何都会被覆盖 copy.appendNone 其他: 复制。附件 ret=[] 我们对所有列表参数进行笛卡尔积 *参数正在分解结构 对于itertools.product*[lst[i]中的项目,用于lindices中的i]: 我们把名单复印一份 curcopy=copy.copy 对于i,拉链中的项目,项目: 我们分配元素 curcopy[i]=项目 我们把副本附在报税表上 复诊 回程网 测试=['A','B',['a0','a1','a2'],'C','D',['b0','b1','b2'],'E','F',['c0','c1','c2']] 印刷体
如果确实希望将返回值中的“a0”包装到数组中,则可以使用ret.append[curcopy]替换ret.appendcurcopy。如果只有一个嵌套级别,则可以这样做:
from itertools import product
lst = ['A', 'B', ['a0', 'a1', 'a2'], 'C', 'D', ['a0', 'a1', 'a2'], 'E', 'F', ['a0', 'a1', 'a2']]
def nested_product(ls):
lst_positions = [l for l in ls if isinstance(l, list)]
for p in product(*lst_positions):
it = iter(p)
yield [e if not isinstance(e, list) else [next(it)] for e in lst]
for pr in nested_product(lst):
print(pr)
输出部分
使用Python 3.3+的替代解决方案如下:
def build_nested_list(ls, it):
return [e if not isinstance(e, list) else [next(it)] for e in ls]
def nested_product(ls):
lst_positions = (li for li in ls if isinstance(li, list))
yield from (build_nested_list(ls, iter(p)) for p in product(*lst_positions))
如果只有一个嵌套级别,则可以执行以下操作:
from itertools import product
lst = ['A', 'B', ['a0', 'a1', 'a2'], 'C', 'D', ['a0', 'a1', 'a2'], 'E', 'F', ['a0', 'a1', 'a2']]
def nested_product(ls):
lst_positions = [l for l in ls if isinstance(l, list)]
for p in product(*lst_positions):
it = iter(p)
yield [e if not isinstance(e, list) else [next(it)] for e in lst]
for pr in nested_product(lst):
print(pr)
输出部分
使用Python 3.3+的替代解决方案如下:
def build_nested_list(ls, it):
return [e if not isinstance(e, list) else [next(it)] for e in ls]
def nested_product(ls):
lst_positions = (li for li in ls if isinstance(li, list))
yield from (build_nested_list(ls, iter(p)) for p in product(*lst_positions))
为什么它在输出数组中为['a0'],而不是仅为'a0'?为什么它在输出数组中为['a0'],而不是仅为'a0'?