将2D数组合并到列表python中 [英] merge 2D array into a list python

查看:75
本文介绍了将2D数组合并到列表python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是像这样旋转2D数组:

What I want to do is to turn a 2D array like this:

np.array([[0,1,2,3], [1,5,6,7]])

np.array([[ 0, 1, 2, 3], [ 1, 5, 6, 7]])

进入此列表(其中包含所有数字的列表):

into this (a list with all numbers in):

[0,1,2,3,1,5,6,7]

[0,1,2,3,1,5,6,7]

有什么办法可以实现?

推荐答案

x = np.array([[ 0, 1, 2, 3], [ 1, 5, 6, 7]])    

list(x.flat)    # if you want a list
#  [0, 1, 2, 3, 1, 5, 6, 7]

x.flatten()  # if you want a numpy array
#   array([0, 1, 2, 3, 1, 5, 6, 7])

我不清楚您要列表还是numpy数组,但是它们都很容易获得(尽管我假设您想要一个列表,因为您用list标记了这个问题).选择任何您想要的或对您最有用的都是合理的.

It's unclear to me whether you want a list or numpy array, but they are both easy to get (although I assume you want a list since you tagged this question with list). It's reasonable to pick whichever you want or is most useful to you.

对于许多用途来说, numpy与列表相比具有明显的优势,但是同样,列表的效果更好.例如,在许多构造中,一次只能获得一个项,并且事先不知道结果输出数组的大小,因此在这种情况下,使用append建立一个列表然后进行转换是有意义的.到一个numpy数组以进行FFT.

For many uses, numpy has significant advantages over lists, but there are also times that lists work better. For example, in many constructions, one gets items one at a time and doesn't know ahead of time the size of the resulting output array, and in this case it can make sense to build a list using append and then convert it to a numpy array to take an FFT.

还有其他方法可以在列表和numpy数组之间进行转换.如果您需要做一些不同的事情(例如更快),请务必查看文档或在此处询问.

There are other approaches to converting between lists and numpy arrays. When you have the need to do things to be different (eg, faster), be sure to look at the documentation or ask back here.

这篇关于将2D数组合并到列表python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆