三重嵌套列表到 python 中的字符串列表

Triple nested list to a list of string in python

我有一个三重嵌套列表,并试图获取最内层列表元素的相同位置,并在 python.

中的第二个嵌套级别加入字符串列表
input=[[['a b c'], ['d e f'], ['g h i']], [['j k l'], ['m n o'], ['p q r']], [['s t u'], ['v w x'], ['y z zz']]]

output=['a b c j k l s t u', 'd e f m n o v w x', 'g h i p q r y z zz']

我找到了如何展平整个列表,但在这种情况下,我喜欢保留第二个内部列表。任何建议表示赞赏!

尝试:

inp = [
    [["a b c"], ["d e f"], ["g h i"]],
    [["j k l"], ["m n o"], ["p q r"]],
    [["s t u"], ["v w x"], ["y z zz"]],
]

out = [" ".join(s for l in t for s in l) for t in zip(*inp)]
print(out)

打印:

["a b c j k l s t u", "d e f m n o v w x", "g h i p q r y z zz"]

您可以使用 itertools.chainmapzip

from itertools import chain
list(map(lambda x: ' '.join(chain(*x)), zip(*my_input)))

输出:

['a b c j k l s t u', 'd e f m n o v w x', 'g h i p q r y z zz']

注意。我将输入命名为 my_list