python列表操作使用示例分享_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语句不对齐导致报错
"""

时间: 2024-09-20 05:55:38

python列表操作使用示例分享_python的相关文章

Python FTP操作类代码分享_python

复制代码 代码如下: #!/usr/bin/py2# -*- coding: utf-8 -*-#encoding=utf-8 '''''    ftp自动下载.自动上传脚本,可以递归目录操作'''  from ftplib import FTPimport os, sys, string, datetime, timeimport socket   class FtpClient:     def __init__(self, host, user, passwd, remotedir, po

python发送邮件接收邮件示例分享_python

接收邮件 复制代码 代码如下: import poplib,pdb,email,re,timefrom email import header POP_ADDR = r'pop.126.com'USER = ''PASS = ''CONFIG = '' def getYear(date):    rslt = re.search(r'\b2\d{3}\b', date)    return int(rslt.group()) def getMonth(date):    monthMap = {

python局域网ip扫描示例分享_python

复制代码 代码如下: #!/usr/bin/python# -*- coding: utf-8 -*- from scapy.all import *from time import ctime,sleepimport threadingTIMEOUT = 4conf.verb=0 def pro(cc,handle): dst = "192.168.1." + str(cc) packet = IP(dst=dst, ttl=20)/ICMP() reply = sr1(packet

python list转dict示例分享_python

需求:['1:a','2:b','3:c'] 转换为 {'1′: 'a','3′: 'c','2′: ''} 复制代码 代码如下: a = {}b = ['1:a','2:b','3:c']map(lambda x:a.setdefault(x.split(':')[0], x.split(':')[1]), b)print a{'1': 'a', '3': 'c', '2': 'b'}

python使用cookielib库示例分享_python

该模块主要功能是提供可存储cookie的对象.使用此模块捕获cookie并在后续连接请求时重新发送,还可以用来处理包含cookie数据的文件. 这个模块主要提供了这几个对象,CookieJar,FileCookieJar,MozillaCookieJar,LWPCookieJar. 1. CookieJar CookieJar对象存储在内存中. 复制代码 代码如下: >>> import urllib2>>> import cookielib>>> c

python实现人人网登录示例分享_python

复制代码 代码如下: import reimport urllib2import cookielib def renren():    cj = cookielib.LWPCookieJar()    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))    email = ''    pwd   = ''     #登录..    print 'login......'    url = "http://www.renr

python抓取网页内容示例分享_python

复制代码 代码如下: import socketdef open_tcp_socket(remotehost,servicename):    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)    portnumber=socket.getservbyname(servicename,'tcp')    s.connect((remotehost,portnumber))    return smysocket=open_tcp_socket

python列表操作实例_python

本文实例讲述了python列表操作的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: class Node:    """Single node in a data structure"""      def __init__(self, data):       """Node constructor"""              self._data = da

Python 列表(List)操作方法详解_python

列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推.Python有6个序列的内置类型,但最常见的是列表和元组.序列都可以进行的操作包括索引,切片,加,乘,检查成员.此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法. 一.创建一个列表只要把逗号分隔的不同的数据项使用方括号括起来即可.如下所示: 复制代码 代码如下: list1