简体   繁体   English

Python Numpy数组转换

[英]Python Numpy array conversion

I've got 2 numpy arrays x1 and x2. 我有2个numpy数组x1和x2。 Using python 3.4.3 使用python 3.4.3

x1 = np.array([2,4,4])
x2 = np.array([3,5,3])

I would like to a numpy array like this: 我想像这样的numpy数组:

[[2,3],[4,5],[4,3]]

How would i go about this? 我怎么会这样呢?

You can use numpy.column_stack : 你可以使用numpy.column_stack

In [40]: x1 = np.array([2,4,4])

In [41]: x2 = np.array([3,5,3])

In [42]: np.column_stack((x1, x2))
Out[42]: 
array([[2, 3],
       [4, 5],
       [4, 3]])

Yep. 是的。 It sounds like zip function: 听起来像拉链功能:

import numpy as np 

x1 = np.array([2,4,4])
x2 = np.array([3,5,3])

print zip(x1, x2) # or [list(i) for i in zip(x1, x2)]

You can zip the 2 arrays like this: 您可以像这样zip 2个数组:

x3 = list(zip(x1,x2))

Output: 输出:

[(2, 3), (4, 5), (4, 3)]

This above code creates a list of tuples . 上面的代码创建了一个tuples list If you want a list of lists , you can use list comprehension : 如果需要list lists ,可以使用list comprehension

x3 = [list(i) for i in list(zip(x1,x2))]

Output: 输出:

[[2, 3], [4, 5], [4, 3]]

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

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