#coding:utf-8

#创建简单的python列表
movies = ["The Holy Grail",
"The Life of Brain",
"The Meaning of Life"]
#可以放在同一行,但分开会更易读
#和数组一样,列表的项从零开始
print movies[1]
#>>>The Life of Brain print movies
#>>>['The Holy Grail', 'The Life of Brain', 'The Meaning of Life'] print len(movies)
#>>>3 #在列表尾部增加append(),或删除pop()数据项,还可以在末尾增加一个数据项集合extend()
movies.pop()
print movies
#>>>['The Holy Grail', 'The Life of Brain']
movies.append("my movie")
print movies
#>>>['The Holy Grail', 'The Life of Brain', 'my movie']
movies.extend(["another movie_1","another movie_2"])
print movies
#>>>['The Holy Grail', 'The Life of Brain', 'my movie', 'another movie_1', 'another movie_2'] #在列表中找到并删除一个特定项remove(),在某个特定项前插入一个数据项insert()
movies.remove("my movie")
print movies
#>>>['The Holy Grail', 'The Life of Brain', 'another movie_1', 'another movie_2']
movies.insert(2,"insert movie")#第一个参数是位置,第二个是要插入的项
print movies
#>>>['The Holy Grail', 'The Life of Brain', 'insert movie', 'another movie_1', 'another movie_2'] #向列表增加更多的数据
#现在我们要向每个电影名后面添加上电影的发行年份,年份是数字,python允许在列表中混合类型
movies = ["The Holy Grail",
"The Life of Brain",
"The Meaning of Life"]
#初始化列表
#方案一:插入年份
movies.insert(1,1975)
movies.insert(3,1979)#注意每插入一项列表长度扩大一
movies.append(1983)
print movies
#>>>['The Holy Grail', 1975, 'The Life of Brain', 1979, 'The Meaning of Life', 1983] movies = ["The Holy Grail",
"The Life of Brain",
"The Meaning of Life"]
#初始化列表
#方案二:从头构造列表
movies = ["The Holy Grail",1975,
"The Life of Brain",1979,
"The Meaning of Life",1983]
print movies
#>>>['The Holy Grail', 1975, 'The Life of Brain', 1979, 'The Meaning of Life', 1983]
'''对于小列表来说,第二种方法更好,比如现在,不必做复杂的运算''' #处理列表数据
#该使用迭代了,for循环可以适用于任意大小的列表
for each_flick in movies:
print each_flick
'''每个输出语句自动换行
>>>
The Holy Grail
1975
The Life of Brain
1979
The Meaning of Life
1983
'''
'''
字符串使用单引号和双引号均可
python大小写敏感
'''
#在列表中存储列表(列表的嵌套)
movies = ['The Holy Grail', 1975, 'The Life of Brain', 1979, ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]
#打印列表中的某一项
print movies[4][1][2]
#>>>Terry Gilliam
#打印嵌套列表中的每一项
for each_item in movies:
print each_item
'''>>>
The Holy Grail
1975
The Life of Brain
1979
['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle']]
'''
#嵌套在内列表的下一层列表回原样打印,需要一种机制来发现列表中的某一项其实好似一个列表
#在列表中查找列表if…else
#使用什么判断条件?python有一个内置BIF可用,是isinstance(),用于检查某个特定标识符是否含有某个特定类型的数据
name = ['Micheael','Terry']
a = isinstance(name,list)
print a
#>>>True
num_name = len(name)
a = isinstance(num_name,list)
print a
#>>>False
#该函数的返回值为True或False
#BIF有71多个,使用dir(__builtins__)查询python的内置方法表,具体查询使用help(某个BIF) #重写使得嵌套列表逐项打印
movies = ['The Holy Grail', 1975, 'The Life of Brain', 1979,
["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]
for item in movies:
if isinstance(item,list):
for item_2 in item:
if isinstance(item_2,list):
for i in item_2:
print i
else:
print item_2
else:
print item
'''
>>>
The Holy Grail
1975
The Life of Brain
1979
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
'''
#但是每增加一层嵌套,就要多写一层重复的代码来进行判断和打印
#不重复代码,应当创建一个函数
#函数需要反复调用,在函数代码组内调用自己
def print_lol(the_list):
for item in the_list:
if isinstance(item,list):
print_lol(item)
else:
print item
print_lol(movies)
'''
>>>
The Holy Grail
1975
The Life of Brain
1979
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
'''
#太棒了,递归不必改变任何代码就可以处理任意深度的嵌套列表
#第一章就到这里结束了=w=

本文原创,转载请注明出处http://www.cnblogs.com/Archimedes/p/7140622.html

【head first python】1.初识python 人人都爱列表的更多相关文章

  1. Python学习笔记1——人人都爱列表

    一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...

  2. Head First Python之人人都爱列表(1-初识Python)

    IDLE 内置函数==BIF==built-in function 默认地,内置函数都是紫色,字符串是绿色,关键字是橙色. tab:自动补全 Alt-P:前一个 Alt-N:下一个 列表的特性 列表看 ...

  3. [Head First Python]1. 初始python-人人都爱列表

    movies = [ "hello", "world",["xin","lover",["Jerry" ...

  4. Python_Day1_人人都爱列表

    列表由一系列按特定顺序排列的元素组成.你可以创建包含字母表中所有字母.数字0~9或 所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有任何关系. 鉴于列表通常包含多个元素,给列 ...

  5. 和我一起学python,初识python (life is short ,we need python)

    作者:tobecrazy  出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 由于项目需要(并非因为life is short), ...

  6. Python基础 初识Python

    机器码 机器码(machine code),学名机器语言指令,有时也被称为原生码(Native Code),是电脑的CPU可直接解读的数据. 通常意义上来理解的话,机器码就是计算机可以直接执行,并且执 ...

  7. python - num1 -初识python

    一.了解python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  8. python基础-初识Python和不同语言之间的区别

    一.Python的创始人谁? Python之父:吉多·范罗苏姆GuidovanRossum 吉多·范罗苏姆是一名荷兰计算机程序员,他作为Python程序设计语言的作者而为人们熟知.在Python社区, ...

  9. Python 1 初识python

    1.Python介绍 Python是一种高级语言,与JAVA C# 等同.可以编写各种应用程序,每种语言都有其合适的应用场景.而Python 的优势在于更加人性化.简便的语法规则,以及针对各种具体场景 ...

随机推荐

  1. 一张图搞定Java设计模式——工厂模式! 就问你要不要学!

    小编今天分享的内容是Java设计模式之工厂模式. 收藏之前,务必点个赞,这对小编能否在头条继续给大家分享Java的知识很重要,谢谢!文末有投票,你想了解Java的哪一部分内容,请反馈给我. 获取学习资 ...

  2. openssl ca(签署和自建CA)

    用于签署证书请求.生成吊销列表CRL以及维护已颁发证书列表和这些证书状态的数据库.因为一般人无需管理crl,所以本文只介绍openssl ca关于证书管理方面的功能. 证书请求文件使用CA的私钥签署之 ...

  3. 在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序

    前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...

  4. ionic 最简单的路由形式,头部固定,下面tab切换-------一个简单的单页切换起飞了

    <ion-header-bar class="bar-dark" align-title="left"> <h1 class="ti ...

  5. (转发)RequestDispatcher的include()方法和forward()方法的区别

    forward(): 该方法用于将请求从一个Servlet传递给服务器上的另外的Servlet.JSP页面或者是HTML文件. 在Servlet中,可以对请求做一个初步的处理,然后调用这个方法,将请求 ...

  6. 一个简单的时间轴demo

    一个时间轴的组成 使用一个块级元素包裹内容,并未块级元素设置边框 定义圆形或者菱形等元素标签,子元素设置偏移或者定位元素将图标定位到边框上 使其中的内容不溢出,自动换行,内容自动撑高 英文自动换行:w ...

  7. Spring Boot 系列(四)静态资源处理

    在web开发中,静态资源的访问是必不可少的,如:图片.js.css 等资源的访问. spring Boot 对静态资源访问提供了很好的支持,基本使用默认配置就能满足开发需求. 一.默认静态资源映射 S ...

  8. Eclipse简单插件开发-启动时间提示

    1.新建Plug-in Project 不用改其他选项,直接点击"Next",然后点击"Finish"   2.新建ShowTime.java package ...

  9. python-web.py 入门介绍

    内容来源:webpy.org 介绍: 1.python中web.py 是一个轻量级Python web框架,它简单而且功能强大.web.py是一个开源项目. 2.安装很简单:pip install w ...

  10. spring加载异常

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...