gpt4 book ai didi

python - 排列嵌套列表中的元组组合

转载 作者:行者123 更新时间:2023-12-01 08:49:32 26 4
gpt4 key购买 nike

我有一个嵌套列表,并且想要排列列表中的组合。

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')),...)

最佳答案

您可以使用递归:

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

编辑:

要查找所有排列,请创建一个方法来查找特定长度的排列,然后迭代输入输入的范围并使用列表理解来获取完整结果:

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:在更新的 test 变量上运行 full_product 时,长度为 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'))
...

关于python - 排列嵌套列表中的元组组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53182808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com