搜索
首页后端开发Python教程Python之列表操作实例教程

Python之列表操作实例教程

Jul 24, 2017 pm 08:32 PM
python实例教程

本文实例讲述了Python列表操作。分享给大家供大家参考,具体如下:

#coding=utf8
'''''
列表类型也是序列式的数据类型,
可以通过下标或者切片操作来访问某一个或者某一块连续的元素。
列表不仅可以包含Python的标准类型,
而且可以用用户定义的对象作为自己的元素。
列表可以包含不同类型的对象,
列表可以执行pop、empt、sort、reverse等操作。
列表可以添加或者减少元素,
还可以与其他列表结合或者把一个列表拆分成几个。
可以对一个元素或者多个元素执行insert、update或者remove操作。
元组和列表主要不同之处在于,前者不可变(只读),
那些用于更新列表的操作,就不能用于元组类型。
列表是由方括号([])来定义的,也可以用工厂方法list()创建它。
可以通过在等号左边指定一个索引或者索引范围的方式来更新一个或几个元素,
也可以通过append()方法追加元素到列表中去。
要删除列表中的元素,如果确切知道要删除元素的索引可以用del语句,
否则可以用remove()方法。
还可以通过pop()方法来删除并从列表中返回一个特定对象。
一般来说,程序员不需要去删除一个列表对象引用。
列表对象出了作用域后它会自动被析构,但如果想删除一整个列表,可以使用del语句。
'''
#创建列表
oneList=["one",1,2,23.6,"two"]
#通过工厂函数创建list
twoList=list("hello world")
#创建一个初始化的表
threeList=[]
#输出列表中的内容
print oneList,"\n",twoList
#访问列表中的元素
#通过索引访问
print oneList[0],oneList[-1]
#通过切片访问,默认间隔为1
print twoList[0:2]
#通过切片访问,设置间隔为2
print twoList[0:5:2]
#更新列表中的元素
#通过索引更新元素
oneList[0]="One"
print oneList[0]
#通过切片更新几个元素
twoList[0:5]=[1,2,3,4,5]
print twoList[0:5]
#调用append()方法,向list中追加元素
threeList.append(oneList)
threeList.append("hello")
print threeList
#删除列表中的元素或列表本身
#del删除列表中某一元素
print len(twoList)
del twoList[5]
print len(twoList),twoList[5]
#remove删除列表中某一元素
print len(threeList)
threeList.remove("hello")
print len(threeList),threeList
#pop删除列表最后一个元素
#并把删除的元素保存为一个对象
print oneList.pop(),oneList
#使用切片删除一定范围内的元素
print twoList
del twoList[0:4]
print twoList
#删除一个列表引用
print twoList
try:
  del twoList
  print twoList
except Exception,e:
  print "twoList not exists"

运行结果:

更多Python相关内容感兴趣的读者可查看本站的内容,感谢大家对我的关注,我会继续努力的。

以上是Python之列表操作实例教程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?May 03, 2025 am 12:11 AM

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

说明如何将内存分配给Python中的列表与数组。说明如何将内存分配给Python中的列表与数组。May 03, 2025 am 12:10 AM

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

您如何在Python数组中指定元素的数据类型?您如何在Python数组中指定元素的数据类型?May 03, 2025 am 12:06 AM

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

什么是Numpy,为什么对于Python中的数值计算很重要?什么是Numpy,为什么对于Python中的数值计算很重要?May 03, 2025 am 12:03 AM

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

讨论'连续内存分配”的概念及其对数组的重要性。讨论'连续内存分配”的概念及其对数组的重要性。May 03, 2025 am 12:01 AM

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

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

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

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

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

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

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

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 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器