【head first python】1.初识python 人人都爱列表
#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 人人都爱列表的更多相关文章
- Python学习笔记1——人人都爱列表
一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...
- Head First Python之人人都爱列表(1-初识Python)
IDLE 内置函数==BIF==built-in function 默认地,内置函数都是紫色,字符串是绿色,关键字是橙色. tab:自动补全 Alt-P:前一个 Alt-N:下一个 列表的特性 列表看 ...
- [Head First Python]1. 初始python-人人都爱列表
movies = [ "hello", "world",["xin","lover",["Jerry" ...
- Python_Day1_人人都爱列表
列表由一系列按特定顺序排列的元素组成.你可以创建包含字母表中所有字母.数字0~9或 所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有任何关系. 鉴于列表通常包含多个元素,给列 ...
- 和我一起学python,初识python (life is short ,we need python)
作者:tobecrazy 出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 由于项目需要(并非因为life is short), ...
- Python基础 初识Python
机器码 机器码(machine code),学名机器语言指令,有时也被称为原生码(Native Code),是电脑的CPU可直接解读的数据. 通常意义上来理解的话,机器码就是计算机可以直接执行,并且执 ...
- python - num1 -初识python
一.了解python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- python基础-初识Python和不同语言之间的区别
一.Python的创始人谁? Python之父:吉多·范罗苏姆GuidovanRossum 吉多·范罗苏姆是一名荷兰计算机程序员,他作为Python程序设计语言的作者而为人们熟知.在Python社区, ...
- Python 1 初识python
1.Python介绍 Python是一种高级语言,与JAVA C# 等同.可以编写各种应用程序,每种语言都有其合适的应用场景.而Python 的优势在于更加人性化.简便的语法规则,以及针对各种具体场景 ...
随机推荐
- Example018主页加载时获取焦点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 6.解决AXIOS的跨域问题
在服务端加上: response.addHeader("Access-Control-Allow-Origin", "*"); response.addHead ...
- html网页的兼容性和css优先级
网页不仅是在一个浏览器上显示的网页,也要多考虑其他浏览器的兼容性,火狐.谷歌.搜狗等浏览器总体来说,网页的变化不大,最主要的是还是IE浏览器. color:red\9; IE6 IE7 IE8 ...
- [图形学] Chp14 GLU曲面裁剪函数程序示例及样条表示遗留问题
样条表示这章已经看完,最后的GLU曲面裁剪函数,打算按书中的示例实现一下,其中遇到了几个问题. 先介绍一下GLU曲面裁剪函数的使用方法. 1 裁剪函数是成对出现的: gluBeginTrim和gluE ...
- (转载)WebSphere MQ安装过程
参考文档: http://www.ibm.com/developerworks/cn/linux/linux-speed-start/l-ss-mq/
- 总结下Redux
Redux 和 React 没有直接关系,它瞄准的目标是应用状态管理. 核心概念是 Map/Reduce 中的 Reduce.且 Reducer 的执行是同步,产生的 State 是 Immutabl ...
- mysql基础篇-----mysql简介
2017-04-19 一.mysql简介 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 ...
- mysql中exists、not exists的用法
exists 关键字是判断是否存在的,存在则返回true,不存在则返回false, not exists则是不存在时返回true,存在返回false: 1. 最常用的if not exists用法: ...
- Redis源码阅读之主从复制——Slave视角
Redis主从复制 为了提高性能和系统可用,Redis都会做主从复制,一来可以分担主库压力,二来在主库挂掉的时候从库依旧可以提供服务.Redis的主从复制是异步复制,返回结果给客户端和同步命令到从库是 ...
- JavaSE中线程与并行API框架学习笔记1——线程是什么?
前言:虽然工作了三年,但是几乎没有使用到多线程之类的内容.这其实是工作与学习的矛盾.我们在公司上班,很多时候都只是在处理业务代码,很少接触底层技术. 可是你不可能一辈子都写业务代码,而且跳槽之后新单位 ...