python 数组添加数组_Python添加到数组[通俗易懂]

python 数组添加数组_Python添加到数组[通俗易懂]python数组添加数组Pythondoesn’thaveanyspecificdatatypeasanarray.WecanuseListthathasallthecharacteristicsofanarray.Python没有任何特定的数据类型作为数组。我们可以使用具有数组所有特征的List。Pythonarraymodulecan…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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 arrayarr1 = 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 + arr2print(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 数组添加数组

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170331.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • jdpaint 精雕入门教程雕花_精雕制图教程视频教程

    jdpaint 精雕入门教程雕花_精雕制图教程视频教程JDPaint5.5北京精雕浮雕建模与编程加工视频教程链接:https://pan.baidu.com/s/1fSQomLrPw4bQtNAZyrPCmQ提取码:7h2m

    2022年9月5日
  • tcptraceroute与traceroute

    tcptraceroute与traceroute1traceroute功能说明:显示数据包到主机间的路径。它默认发送的数据包大小是40字节。通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径。当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样,但基本上来说大部分时候所走的路由是相同的。工作原理Traceroute程序的…

    2022年6月20日
  • IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结

    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结IntelliJIDEA使用教程(总目录篇)首先,使用的时候,自己得先在电脑上安装个小乌龟。也就是svn啦。第一步安装小乌龟。如下:具体安装好像没什么具体要求,一路next,就好。如上图箭头所示,在安装TortoiseSVN的时候,默认commandlineclienttools,是不安装的,这里建议勾选上。这个我不确定我当时选没选,不过呢,你给安装…

    2022年5月15日
  • SCSA—信息安全概述

    SCSA—信息安全概述数字化时代威胁升级:攻击频发、传统安全防护逐渐失效、安全风险能见度低、缺乏自动化防御手段一、信息安全概述:1)信息安全:防止任何对数据进行未授权访问的措施,或者防止造成信息有意无意泄漏、破坏、丢失等问题的发生,让数据处于远离危险、免于威胁的状态或特性2)网络安全:计算机网络环境下的信息安全二、信息安全的脆弱性及常见安全攻击1.网络的开放性:互联网的美妙之处在于你与每一个相连,它的可怕之处在于每一个人与你相连2.协议栈的脆弱性及常见攻击1)协议栈的自身脆弱性:缺乏数据源验证机制、缺乏机密性保障机

    2022年6月20日
  • execute executeupdate_executeupdate不执行

    execute executeupdate_executeupdate不执行增、删、改用executeUpdate()返回值为int型,表示被影响的行数例子查用executeQuery()返回的是一个集合.next()表示指针先下一行,还有first()指向第一行、last()指向最后一行、如果有数据就返回true…

    2022年8月31日
  • 关于HTTP请求出现 405状态码 not allowed的解决办法

    关于HTTP请求出现 405状态码 not allowed的解决办法

    2021年10月12日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号