搜索
首页后端开发Python教程numpy实现合并多维矩阵、list的扩展方法

numpy实现合并多维矩阵、list的扩展方法

May 08, 2018 pm 04:11 PM
numpy多维矩阵

这篇文章主要介绍了关于numpy实现合并多维矩阵、list的扩展方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

一、合并多个numpy矩阵

1、首先创建两个多维矩阵

矩阵a的大小为(2,3,2)

矩阵b的大小为(3,2,3)

采用concatentate这个函数就可以合并两个多维矩阵

合并之后应为(5,3,2)

In [1]: import numpy as np
In [2]: a = np.ndarray((3, 2, 3))
In [3]: b = np.ndarray((2, 2, 3))
In [4]: print(a.shape, b.shape)
(3, 2, 3) (2, 2, 3)
In [5]: c = np.concatenate((a, b), axis = 0)
In [6]: print(c.shape)
(5, 2, 3)
In [7]:

二、矩阵的追加

矩阵的追加是采用append这个函数,list也有这个函数,但是二者的使用方式略有不同。

1、创建一个ndarray

2、然后使用np.append()函数进行追加(注意是np.append, 不是a.append)

In [2]: import numpy as np
In [3]: a = np.array([1, 2, 3, 4, 5])
In [4]: a = np.append(a, 10)
In [5]: a
Out[5]: array([ 1, 2, 3, 4, 5, 10])
In [6]: a = np.append(a, [1, 2, 3])
In [7]: a
Out[7]: array([ 1, 2, 3, 4, 5, 10, 1, 2, 3])

三、列表的扩展(extend)

1、列表的扩展就是把两个列表合并

2、采用extend函数

In [9]: a = [1, 2, 3, 4]
In [10]: b = [5, 6, 7, 8]
In [11]: a
Out[11]: [1, 2, 3, 4]
In [12]: b
Out[12]: [5, 6, 7, 8]
In [13]: c = a.extend(b)
In [14]: c
In [15]: a
Out[15]: [1, 2, 3, 4, 5, 6, 7, 8]

请注意extend这个函数的返回值是None,所以上面第13行c的输出为空,而a的值已经变了,所以它是直接在a后面扩展的,并没有任何返回值。

四、列表的追加

列表的追加直接用append就行

1、创建列表a

2、在a的后面追加数据

In [28]: a = [1, 2,3,4]
In [29]: a.append(6)
In [30]: a
Out[30]: [1, 2, 3, 4, 6]
In [31]:

相关推荐:

如何实现删除numpy.array中的行或列

以上是numpy实现合并多维矩阵、list的扩展方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在Python阵列上可以执行哪些常见操作?在Python阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

您如何在python列表中访问元素?您如何在python列表中访问元素?Apr 26, 2025 am 12:03 AM

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Python的科学计算中如何使用阵列?Python的科学计算中如何使用阵列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何处理同一系统上的不同Python版本?您如何处理同一系统上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

与标准Python阵列相比,使用Numpy数组的一些优点是什么?与标准Python阵列相比,使用Numpy数组的一些优点是什么?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器