python列表操作使用示例分享
更新时间:2014年02月21日 15:31:05 作者:
这篇文章主要介绍了python列表操作使用示例,需要的朋友可以参考下
复制代码 代码如下:
Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> cast=["cleese","palin","jones","idle"]
>>> print(cast)
['cleese', 'palin', 'jones', 'idle']
>>> print(len(cast))#显示数据项数量
4
>>> print(cast[1])#显示列表中第2个数据项的值
palin
>>> cast.append("gilliam")#在列表末尾添加一个数据项
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam']
>>> cast.pop()#删除列表末尾的数据项
'gilliam'
>>> print(cast)
['cleese', 'palin', 'jones', 'idle']
>>> cast.extend(["gilliam","chapman"])#在列表末尾增加一个数据项集合
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam', 'chapman']
>>> cast.remove("chapman")#删除指定的数据项
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam']
>>> cast.insert(0,"chapman")#在指定的位置增加数据项
>>> print(cast)
['chapman', 'cleese', 'palin', 'jones', 'idle', 'gilliam']
>>>
下面是讲定义一个def函数,isinstance()函数,for in,if else等的运用以及逻辑
复制代码 代码如下:
movies=["the holy grail",1975,"terry jone & terry gilliam",91,
["graham chapman",
["michael palin","john cleese","terry gilliam",
"eric idle","terry jones"]]]
def print_lol(the_list):#定义一种函数
for each_item in the_list:#for in循环迭代处理列表,从列表起始位置到末尾
if isinstance(each_item,list):#isinstance()检测each_item里每一项
#是不是list类型
print_lol(each_item)#如果是,调用函数print_lol
else:print(each_item)#如果不是,输出这一项
print_lol(movies)#在movies列表中调用函数
"""
之前if else语句不对齐导致报错
"""
相关文章
pip 20.3 新版本发布!即将抛弃 Python 2.x(推荐)
这篇文章主要介绍了pip 20.3 新版本发布!即将抛弃 Python 2.x,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12
Python的Bottle框架中返回静态文件和JSON对象的方法
这篇文章主要介绍了Python的Bottle框架中返回静态文件和JSON对象的方法,Bottle框架在Python开发者中具有很高的人气,需要的朋友可以参考下2015-04-04
Python语法糖for else循环语句里的break使用详解
这篇文章主要介绍了Python语法糖之for else循环语句里的break使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-05-05


最新评论