这篇文章主要介绍了Python中list以及list与array的相互转换实现方法,简单分析了Python中list的功能、使用方法及list与array相互转换实现技巧,需要的朋友可以参考下
本文实例讲述了Python中list以及list与array的相互转换实现方法。分享给大家供大家参考,具体如下:
python中的list是一种有序集合,可以随时增删元素;
# -*- coding: utf-8 -*- frameID = 1 frameID_list = [] frameID_list.append(frameID) print (frameID_list) frameID = 2 frameID_list.append(frameID) print (frameID_list) name = 'SRX' frameID_list.append(name) print (frameID_list) print (len(frameID_list)) #打印长度 print (frameID_list[2]) #使用[]进行索引 print (frameID_list[-1]) frameID_list.insert(1, 'WJC') #在指定位置插入元素 print (frameID_list) frameID_list.pop() #将最后一位的元素删除 print (frameID_list) frameID_list.pop(0) #删除指定位置的元素 print (frameID_list)
下面说一下list与numpy中的array相互转换。
首先list中的元素属性应该一致才能转换,如下所示,其中frame_ID_list中包含的元素是str型记录的帧号,首先利用np.array将其转成int型的array,然后将转换之后的数组每个元素都做了一个数学运算,由于运算之后的结果是float,因此在第二行又将其强转成了int型,最后再将np的数组转回list,整个操作相当于完成了对list(其中包含的是str类型的元素)中的每个元素进行数学运算,然后再转回list的过程。
frame_ID_list_np = np.array(frame_ID_list, dtype = int) frame_ID_list_np = np.array(frame_ID_list_np / 1000 * fps, dtype = int) frame_ID_list = frame_ID_list_np.tolist()
另外,还有一个需要注意的地方,就是类型转换之后再判断两个变量是否相等时,例如:
frame_ID = '1000' ID_count = 1000 frame_ID = '1000' ID_count = 1000 if frame_ID == ID_count: ...
这时这个if是不会成立的,这么直接写出来就很容易看到错误所在,但是当frame_ID与ID_count是从别的变量获得的,就没有那么清晰的看出两者的类型了,所以在编程的时候一定要注意这些小细节;
类似的还有存储文件的时候,比如:
str_name_FID = str(frame_ID_list_np_save[count_save_frame_np]) cv2.imwrite('image/' + video_name_save + '_' + str_name_FID + '.jpg',frame) #save the image count_save_frame_np = count_save_frame_np + 1
在存储图片时,图片的名字一定都要是str类型相互串在一起的,count_save_frame_np是int型的变量,如果直接在程序中使用该变量作为图片名的一部分,是不会存下来任何文件的,所以第一行的强转是必须的,先将其转成str型的,再使用就可以了。
以上是Python实现列表list以及list与数组array的相互转换的方法介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版
好用的JavaScript开发工具

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器