python列表操作使用示例分享

yipeiwu_com6年前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语句不对齐导致报错
"""

相关文章

python实现名片管理系统项目

python实现名片管理系统项目

名片管理系统,供大家参考,具体内容如下 环境要求:linux系统、python2.x或python3.x 资源地址:GitHub地址 写在前面的话:笔者之前在初学C/C++时,都曾写过类...

pandas.read_csv参数详解(小结)

pandas.read_csv参数整理  读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata...

使用Python paramiko模块利用多线程实现ssh并发执行操作

1.paramiko概述 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography)。 有了Pa...

Python3中的真除和Floor除法用法分析

本文实例讲述了Python3中的真除和Floor除法用法。分享给大家供大家参考,具体如下: 在Python3中,除法运算有两种,一种是真除,一种是Floor除法,这两者是有分别的,分别如...

1分钟快速生成用于网页内容提取的xslt

1分钟快速生成用于网页内容提取的xslt

1分钟快速生成用于网页内容提取的xslt,具体内容如下 1、项目背景 在《Python即时网络爬虫项目说明》一文我们说过要做一个通用的网络爬虫,而且能节省程序员大半的时间,而焦点问题就是...