#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. 【Android Developers Training】 106. 创建并检测地理围栏

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 41. 向另一台设备发送文件

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. Mac用ssh登录Ubuntu14.04

    在Ubuntu上配置ssh-server sudo apt-get install openssh-server  然后确认ssh-server是否启动  ps -e | grep ssh 如果存在s ...

  4. Linux中使用京东代码库JDCode创建私有Git仓库

    国外Git经常被墙,所以目光转向国内.目前,云存储真的是很热,有很多公司在做. 看了一下,CSDN,开源中国,淘宝,京东,Gitcafe都在搞.淘宝只支持SVN. JD号称提供1G免费空间,而且支持私 ...

  5. Angular 4 学习笔记 从入门到实战 打造在线竞拍网站 基础知识 快速入门 个人感悟

    最近搞到手了一部Angular4的视频教程,这几天正好有时间变学了一下,可以用来做一些前后端分离的网站,也可以直接去打包web app. 环境&版本信息声明 运行ng -v @angular/ ...

  6. KBEngine简单RPG-Demo源码解析(1)

    一:环境搭建1. 确保已经下载过KBEngine服务端引擎,如果没有下载请先下载          下载服务端源码(KBEngine):              https://github.com ...

  7. (转)ManyToMany注解

    @ManyToMany  注释:表示此类是多对多关系的一边,mappedBy 属性定义了此类为双向关系的维护端,注意:mappedBy 属性的值为此关系的另一端的属性名. 例如,在Student类中有 ...

  8. centos7安装nodejs

    方法一.https://github.com/nodesource/distributions#rpminstall 按照上面地址中的教程安装完后,使用node -v命令报错: -bash: /usr ...

  9. MongoDB 安装和配置

    [前言] Mongodb是一款nosql数据库,关于nosql 以及 mongodb本文不进行介绍,在数据库的选型方面,本人说是在机缘巧合之下选择了mongodb,并且拟使用mongodb搭建日志系统 ...

  10. VMware虚拟机下Ubuntu连不上网解决

    虚拟机:VMware® Workstation 12 Pro Ubuntu: 14.04 问题描述:电脑从公司拿到家打开Ubuntu之后,打开自带Firefox浏览器,显示连接不上网,终端下ping ...