简体   繁体   English

在python中合并1D和2D列表

[英]Merging 1D and 2D lists in python

I'm trying to set up data to convert to a numpy array. 我正在尝试设置数据以转换为numpy数组。 I have three lists. 我有三个清单。 Two are one dimensional, and one is two dimensional. 两个是一维的,一个是二维的。

a = [1,2,3]
b = [4,5,6]
c = [ [7,8],[9,10],[11,12] ]

I want to end up with this: 我想最终得到这个:

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

I've tried using zip() , but it doesn't delve into the 2D array. 我尝试过使用zip() ,但它没有深入研究2D数组。

Assuming you don't mind if the use of NumPy in the conversion itself, the following should work. 假设您不介意在转换本身中使用NumPy,以下内容应该可行。

from numpy import array

a = array([1, 2, 3])
b = array([4, 5, 6])
c = array([[7, 8], [9, 10], [11, 12]])

result = array(list(zip(a, b, c[:, 0], c[:, 1])))

Note that c[:, n] will only work with NumPy arrays, not standard Python lists. 请注意, c[:, n]仅适用于NumPy数组,而不适用于标准Python列表。

A numpy solution (as per your tag) would be 一个numpy解决方案(根据你的标签)将是

In [398]: np.vstack([a,b,np.array(c).T]).T
Out[398]: 
array([[ 1,  4,  7,  8],
       [ 2,  5,  9, 10],
       [ 3,  6, 11, 12]])

this makes c into a 2x3 array: 这使得c变为2x3数组:

In [399]: np.array(c).T
Out[399]: 
array([[ 7,  9, 11],
       [ 8, 10, 12]])

which can then be stacked (concatenated) vertically with a and b which also have 3 elements. 然后可以与ab垂直堆叠(连接), ab也有3个元素。

zip(*c) is a list form of transpose zip(*c)是转置的列表形式

In [412]: list(zip(a,b,c)) 
Out[412]: [(1, 4, [7, 8]), (2, 5, [9, 10]), (3, 6, [11, 12])]

In [418]: list(zip(a,b,*zip(*c)))
Out[418]: [(1, 4, 7, 8), (2, 5, 9, 10), (3, 6, 11, 12)]

With python3 it's as simple as 使用python3就像它一样简单

a = [1,2,3]
b = [4,5,6]
c = [ [7,8],[9,10],[11,12] ]
[[x, y, *z] for x, y, z in zip(a, b, c)]
[[1, 4, 7, 8], [2, 5, 9, 10], [3, 6, 11, 12]]

In case you want to operate with standard Python lists you can use zip and then process each row afterwards: 如果您想使用标准Python列表进行操作,可以使用zip然后处理每一行:

from itertools import chain

a = [1,2,3]
b = [4,5,6]
c = [[7,8], [9,10], [11,12]]

[list(chain.from_iterable(y if isinstance(y, list) else [y] for y in x)) for x in zip(a, b, c)]

For starters, zip yields tuples, not lists, so you'd have to convert its output into lists: 对于初学者来说, zip产生元组而不是列表,因此您必须将其输出转换为列表:

def lzip(*args):
    'Transforms the tuples yielded by zip into equivalent lists.'
    for tup in zip(*args):
        yield list(tup)
    return

# Later...
a = [1, 2, 3]
b = [4, 5, 6]
c = [
  [7, 8],
  [9, 10],
  [11, 12],
  ]
lzip(a, b)
# A generator that yields: [1, 4], [2, 5], [3, 6]

Second, zip won't return what you want no matter what, because the operation you're trying to perform on the sub-lists of c is concatenation , not zipping. 其次, zip不会返回你想要的东西,因为你试图在c的子列表上执行的操作是连接 ,而不是压缩。

First, lzip the 1-d lists as in the example above, then concatenate each sublist that results with the matching sublist in c . 首先, lzip例所示, lzip 1-d列表,然后将每个子列表与c的匹配子列表连接起来。

Below, I've used a list comprehension to build the combined list assign it to a variable, with two new local variables to make the zip operation more clear: 下面,我使用列表推导来构建组合列表,将其分配给变量,使用两个新的局部变量使zip操作更清晰:

heads = lzip(a, b)
tails = c
zipped = [head + tail for head, tail in zip(heads, tails)]
# [[1, 4, 7, 8], [2, 5, 9, 10], [3, 6, 11, 12]]

The zip yields 2-tuples containing the n ⁠th head and n ⁠th tail: zip产生2元组,包含n个第 4个头和第n个尾部:

# First iteration:
([1, 4], [7, 8])
# Next iteration:
([2, 5], [9, 10])
# Last iteration:
([3, 6], [11, 12])

These tuples are unpacked into two variables by the (invisible) assignment head, tail = ... inside the list comprehension. 这些元组通过(不可见)赋值head, tail = ...在列表理解中解压缩为两个变量。 The tuples themselves are then thrown away. 然后扔掉元组本身。

Concatenating ("adding") lists with head + tail combines two lists into one. head + tail连接(“添加”)列表将两个列表合并为一个。 In this case, pairs of 2-element lists become individual 4-element lists. 在这种情况下,成对的2元素列表成为单独的4元素列表。

Finally the [ and ] of the list comprehension collect the 4-element lists into a single list... of 4-element lists. 最后,列表推导的[]将4元素列表收集到4个元素列表的单个列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM