python 数组添加数组_Python添加到数组

python 数组添加数组

Python doesn’t have any specific data type as an array. We can use List that has all the characteristics of an array.

Python没有任何特定的数据类型作为数组。 我们可以使用具有数组所有特征的List。

Python array module can be used to create an array of integers and floating-point numbers.

Python数组模块可用于创建整数和浮点数的数组。

If you want to do some mathematical operations on an array, you should use the NumPy module.

如果要对数组进行一些数学运算,则应使用NumPy模块。

1. Python添加到数组 (1. Python add to Array)

  • If you are using List as an array, you can use its append(), insert(), and extend() functions. You can read more about it at Python add to List.

    如果将List用作数组,则可以使用其append(),insert()和extend()函数。 您可以在Python add to List中阅读有关它的更多信息。
  • If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array.

    如果使用的是数组模块,则可以使用+运算符,append(),insert()和extend()函数进行串联,以将元素添加到数组中。
  • If you are using NumPy arrays, use the append() and insert() function.

    如果您使用的是NumPy数组,请使用append()和insert()函数。

2.使用数组模块将元素添加到数组 (2. Adding elements to an Array using array module)

  • Using + operator: a new array is returned with the elements from both the arrays.

    使用+运算符:返回一个新数组,其中包含两个数组中的元素。
  • append(): adds the element to the end of the array.

    append():将元素添加到数组的末尾。
  • insert(): inserts the element before the given index of the array.

    insert():将元素插入到数组的给定索引之前。
  • extend(): used to append the given array elements to this array.

    extend():用于将给定的数组元素附加到此数组。
import array

arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])

print(arr1)  # array('i', [1, 2, 3])
print(arr2)  # array('i', [4, 5, 6])

arr3 = arr1 + arr2
print(arr3)  # array('i', [1, 2, 3, 4, 5, 6])

arr1.append(4)
print(arr1)  # array('i', [1, 2, 3, 4])

arr1.insert(0, 10)
print(arr1)  # array('i', [10, 1, 2, 3, 4])

arr1.extend(arr2)
print(arr1)  # array('i', [10, 1, 2, 3, 4, 4, 5, 6])

3.向NumPy数组添加元素 (3. Adding elements to the NumPy Array)

  • append(): the given values are added to the end of the array. If the axis is not provided, then the arrays are flattened before appending.

    append():将给定值添加到数组的末尾。 如果未提供轴,则在附加之前将阵列弄平。
  • insert(): used to insert values at the given index. We can insert elements based on the axis, otherwise, the elements will be flattened before the insert operation.

    insert():用于在给定索引处插入值。 我们可以基于轴插入元素,否则,将在插入操作之前将元素展平。
>>> import numpy as np
>>> np_arr1 = np.array([[1, 2], [3, 4]])
>>> np_arr2 = np.array([[10, 20], [30, 40]])
>>> 
>>> np.append(np_arr1, np_arr2)
array([ 1,  2,  3,  4, 10, 20, 30, 40])
>>>
>>> np.append(np_arr1, np_arr2, axis=0)
array([[ 1,  2],
       [ 3,  4],
       [10, 20],
       [30, 40]])
>>>
>>> np.append(np_arr1, np_arr2, axis=1)
array([[ 1,  2, 10, 20],
       [ 3,  4, 30, 40]])
>>> 
>>> np.insert(np_arr1, 1, np_arr2, axis=0)
array([[ 1,  2],
       [10, 20],
       [30, 40],
       [ 3,  4]])
>>> 
>>> np.insert(np_arr1, 1, np_arr2, axis=1)
array([[ 1, 10, 30,  2],
       [ 3, 20, 40,  4]])
>>>

4.参考 (4. References)

翻译自: https://www.journaldev.com/33185/python-add-to-array

python 数组添加数组

  • 10
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,如果你使用的是列表作为数组,你可以使用append()函数来向数组添加元素。通过调用append()函数并传入要添加的元素作为参数,可以将元素添加列表的末尾。例如,如果你有一个列表x = \[1, 2, 3, 4, 5, 6, 7, 8, 9\],并且想要向其中添加元素2,你可以使用append()函数来实现。具体代码如下所示: x = \[1, 2, 3, 4, 5, 6, 7, 8, 9\] x.append(2) print(x) 输出结果为:\[1, 2, 3, 4, 5, 6, 7, 8, 9, 2\]。\[1\] 另外,如果你想要使用NumPy模块对数组进行数学运算,你可以使用NumPy的append()函数。这个函数将给定的值添加数组的末尾。如果没有提供轴参数,那么在添加之前,数组会被展平。\[2\] #### 引用[.reference_title] - *1* [Python中的append()方法](https://blog.csdn.net/weixin_45272908/article/details/118069860)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python 数组添加数组_Python添加数组](https://blog.csdn.net/cunchi4221/article/details/107471899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值