[英]Appending multiple elements to numpy array
I have 8
elements and I want to add them into a array in numpy.我有
8
元素,我想将它们添加到 numpy 中的数组中。 I used np.append()
but it seems that I can only add two elements at one time.我使用了
np.append()
但似乎一次只能添加两个元素。 I want to add all 8
elements at once.我想一次添加所有
8
元素。 first_1 =35.72438966508524
, first_2 = 35.73839550991734
, etc. first_1 =35.72438966508524
, first_2 = 35.73839550991734
等
35.72438966508524 35.73839550991734 35.81944190992304
35.80549149559467 35.78399019604507 36.03781192909738
35.9957696566448 35.94692998938782
np.append(first_1,first_2,first_3,first_4,first_5,first_6,first_7,first_8)
The error is错误是
TypeError: append() takes from 2 to 3 positional arguments but 8 were given
np.array([first_1,first_2,first_3,first_4,first_5,first_6,first_7,first_8])
Correct syntax is (if I suppose that you wanted to append your 8 values to an numpy array named as ar
:正确的语法是(如果我假设您想将 8 个值附加到名为
ar
的 numpy 数组:
np.append(ar, (first_1, first_2, first_3, first_4, first_5, first_6, first_7, first_8))
first = 35.72438966508524
second = 35.73839550991734
third = 35.81944190992304
forth = 35.80549149559467
fifth = 35.78399019604507
sixth = 36.03781192909738
seventh = 35.9957696566448
eighth = 35.94692998938782
now to make a new numpy array:现在创建一个新的 numpy 数组:
a = np.array([first, second, third, forth, fifth, sixth, seventh, eighth])
output:输出:
a
Out[89]:
array([35.72438967, 35.73839551, 35.81944191, 35.8054915 , 35.7839902 ,
36.03781193, 35.99576966, 35.94692999])
to append to existing array (using previously created 'a'):附加到现有数组(使用先前创建的“a”):
a = np.append(a, [first, second, third, forth, fifth, sixth, seventh, eight], axis=0)
which gives:这使:
a
Out[93]:
array([35.72438967, 35.73839551, 35.81944191, 35.8054915 , 35.7839902 ,
36.03781193, 35.99576966, 35.94692999, 35.72438967, 35.73839551,
35.81944191, 35.8054915 , 35.7839902 , 36.03781193, 35.99576966,
35.94692999])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.