Python 如何组合不同列表中的元素

Python 如何组合不同列表中的元素,python,Python,我试图使用下面的代码来组合两个列表之间的元素 输入: nested_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 我希望得到以下结果: [[1, 4, 5, 6], [2, 4, 5, 6], [3, 4, 5, 6], [1, 7, 8, 9], ... [6, 7, 8, 9]] 我尝试使用下面的格式,但它似乎没有以我想要的格式输出 你能帮忙吗 def unique_combination(nested_array): try:

我试图使用下面的代码来组合两个列表之间的元素

输入:

nested_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
我希望得到以下结果:

[[1, 4, 5, 6], [2, 4, 5, 6], [3, 4, 5, 6], [1, 7, 8, 9], ... [6, 7, 8, 9]]
我尝试使用下面的格式,但它似乎没有以我想要的格式输出

你能帮忙吗

def unique_combination(nested_array):
    try:
        for n1, array in enumerate(nested_array):
            for element in array:
                a = [element], list(nested_array[n1+1])
                print(a)
    except IndexError:
        pass
另外,我没有使用print(),而是尝试使用return操作。 但对于return操作,它只返回一个输出。
我的编码正确吗?

在不知道所需的全部输出的情况下,这看起来可以满足您的需要。请注意,这可以使用列表理解写在一行中,但这是一行令人讨厌的内容

nested_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for list_index in range(len(nested_array) - 1):
    for marcher_index in range(list_index + 1, len(nested_array)):
        for ele in nested_array[list_index]:
            print([ele] + nested_array[marcher_index])
输出:

[1, 4, 5, 6]
[2, 4, 5, 6]
[3, 4, 5, 6]
[1, 7, 8, 9]
[2, 7, 8, 9]
[3, 7, 8, 9]
[4, 7, 8, 9]
[5, 7, 8, 9]
[6, 7, 8, 9]
试试这个:

flatte\u list=lambda ls:[子列表中的子列表中的项对于子列表中的项]
嵌套数组=[[1,2,3],[4,5,6],[7,8,9]]
唯一_组合=[[i]+范围内j的嵌套_数组[j](1,len(嵌套_数组))
对于展平列表中的i(嵌套数组[:j])]
打印(唯一的\u组合)
输出

[[1, 4, 5, 6],
 [2, 4, 5, 6],
 [3, 4, 5, 6],
 [1, 7, 8, 9],
 [2, 7, 8, 9],
 [3, 7, 8, 9],
 [4, 7, 8, 9],
 [5, 7, 8, 9],
 [6, 7, 8, 9]]

Return标记函数存储值的点,供调用方使用。它不会打印任何内容。在不了解代码的情况下尝试使用代码会导致问题,尝试自己想出一些东西是有效的输出吗?@python\u user\u learn,您是否检查了
itertools.compositions
iterable
创建唯一组合?。请发布更多关于如何解决这个问题的详细信息。