蟒蛇,numpy的;如何在一个数组的开始插入元素 [英] python, numpy; How to insert element at the start of an array
本文介绍了蟒蛇,numpy的;如何在一个数组的开始插入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有复数的numpy的数组。所以我想在数组中开始插入零,
和该阵列一个地方的其余部分向前移动。结果
I have an numpy array of complex numbers. So I want to insert zero at start of the array,
and shift the rest of the array one place forward.
例如:结果
a = [1 + 2j, 5 + 7j,..]
我想打:
a = [0 + 0j, 1 + 2j, 5 + 7j,..]
什么是做到这一点最简单的方法?
What's the simplest way to do this?
推荐答案
最简单的方法:
a = np.array([1 + 2j, 5 + 7j])
a = np.insert(a, 0, 0)
然后:
>>> a
array([ 0.+0.j, 1.+2.j, 5.+7.j])
请注意,这将创建一个新的数组,实际上它并不插入 0
成原来的数组。
Note that this creates a new array, it does not actually insert the 0
into the original array.
有几个选择到 np.insert
,所有这些都还可以创建一个新的数组:
There are several alternatives to np.insert
, all of which also create a new array:
In [377]: a
Out[377]: array([ 1.+2.j, 5.+7.j])
In [378]: np.r_[0, a]
Out[378]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [379]: np.append(0, a)
Out[379]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [380]: np.concatenate([[0], a])
Out[380]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [381]: np.hstack([0, a])
Out[381]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
In [382]: np.insert(a, 0, 0)
Out[382]: array([ 0.+0.j, 1.+2.j, 5.+7.j])
这篇关于蟒蛇,numpy的;如何在一个数组的开始插入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文