简明python教程五----数据结构
python中有三种内建的数据结构:列表、元组和字典
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。在python中,每个项目之间用逗号分隔。
列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。一旦你创建了一个列表,可以添加、删除或者搜索列表中的项目。
列表是可变的数据类型,这种类型是可以被改变的。
python为list类提供了append方法,让你在列表尾添加一个项目。
如mylist.append('an item')列表mylist中增加那个字符串。注意使用点号来使用对象的方法。
#!/usr/bin/python
#Filename:using_list.py #This is my shopping list
shoplist=['apple','mango','carrot','banana']
print 'I have',len(shoplist),'items to purchase.' print 'These items are:',
for item in shoplist:
print item, print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now',shoplist print 'I will sort my list now'
shoplist.sort()
print 'sorted shopping list is',shoplist print 'The first item T will buy is',shoplist[]
olditem=shoplist[]
del shoplist[]
print 'I bought the',olditem
print 'My shopping list is now',shoplist
结果:
I have items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
shoplist是购物列表,在shoplist列表中可以添加任何种类的对象包括数甚至其他列表。
我们使用列表的sort方法来对列表排序。注意:这个方法影响列表本身,而不是返回一个修改后的列表。(列表可变而字符串是不可变的)
元组
元组和列表十分类似。只不过元组和字符串一样是不可变的,即你不能修改元组。
元组通过圆括号中用逗号分隔的项目定义。元组的值不会改变
zoo = ('wolf','elephant','penguin')
print 'Number of animals in the zoo is',len(zoo)
new_zoo = ('monkey','dolphin',zoo)
print 'Number of animals in the new zoo is',len(new_zoo)
print 'All animals in new zoo are',new_zoo
print 'Animals brought from old zoo are',new_zoo[]
print 'Last animal brought from old zoo is',new_zoo[][]
输出结果:
Number of animals in the zoo is
Number of animals in the new zoo is
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
len函数可以用来获取元组的长度。
通过一对方括号来指明某个项目的位置来访问元组中的项目,称为索引运算符,
含有0个或1个项目的元组,空的元组由一对空的圆括号组成,如myempty=()
含有单个元素的元组:你必须在第一个(唯一一个)项目后跟一个逗号,python才能区分元组和表达式中一个带圆括号的对象。如singleton=(2,)。
列表之中的列表不会失去它的身份,同样元组中的元组,或列表中的元组,他们就是使用一个对象存储的对象。
元组与打印语句
age =
name = 'Swaroop' print '%s is %d years old'%(name,age)
print 'why is %s playing with that python?'%name
print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。
定制可以是%s表示字符串或%d表示整数。
在第二个print语句中,我们使用了一个定制,,后面跟着%符号后的单个项目----没有圆括号。这只在字符串中只有一个定制的时候有效
字典
字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿。我们把键(名字)值(详细情况)联系在一起。
注意:键必须唯一。
只能使用不可变的对象(比如字符串)来作为字典的键,可以把不可变或可变的对象作为字典的值。
键值对在字典中标记方式:d={key1:value1,key2:value2},注意键/值对用冒号分隔,而各个对用逗号分隔,所以这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对他们排序
字典是dict类的实例/对象
#'ab' is short for 'a' ddress 'b' ook
ab={'Swaroop':'swaroopch@byteofpython.info',
'Larry':'larry@wall.org',
'Matsumoto':'matz@ruby-lang.org',
'Spammer':'spammer@hotmial.com'
}
print "Swaroop's address is %s"%ab['Swaroop'] ab['Guido']='guido@python.org' del ab['Spammer']
print '\nThere are %d contacts in the address-book\n'%len(ab)
for name,address in ab.items():
print 'Contact %s at %s' %(name,address) if 'Guido' in ab:
print "\nGuido's address is %s"%ab['Guido']
序列
列表、元组和字符串都是序列,序列的两个主要特点是索引操作符和切片操作符
切片操作符:让我们获取序列的一个切片,即一部分序列。
shoplist = ['apple','mango','carrot','banana']
print "Item 0 is",shoplist[]
print "Item 1 is",shoplist[]
print "Item 2 is",shoplist[]
print "Item 3 is",shoplist[]
print "Item -1 is",shoplist[-]
print "Item -2 is",shoplist[-] print "Item 1 to 3 is",shoplist[:]
print "Item 2 to end is",shoplist[:]
print "Item 1 to -1 is",shoplist[:-]
print "Item start to end is",shoplist[:] name = 'swaroop'
print 'characters 1 to 3 is',name[:]
print 'characters 2 to end is',name[:]
print 'characters 1 to -1 is',name[:-]
print 'characters start to end is',name[:]
结果:
Item is apple
Item is mango
Item is carrot
Item is banana
Item - is banana
Item - is carrot
Item to is ['mango', 'carrot']
Item to end is ['carrot', 'banana']
Item to - is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters to is wa
characters to end is aroop
characters to - is waroo
characters start to end is swaroop
使用索引来取得序列中的单个项目,例如shoplist[1]
索引同样可以是负数,位置从序列尾开始计算的。因此shoplist[-1]表示序列的最后一个。
切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并且冒号分隔。记住数时可选的,而冒号是必须的。
切片操作符的第一个数(冒号前)表示切片开始的位置,第二个数(冒号后)表示切片到哪里结束。
不指明第一个数,python就从序列首开始,不指明第二数,python会停止在序列尾。
注意:返回的序列从开始的位置开始,在结束位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。
简明python教程五----数据结构的更多相关文章
- 简明python教程五----数据结构(下)
引用 当你创建一个对象并给它赋一个变量的时候,这个变量仅仅引用那个对象,而不是表示这个对象本身!即,变量名指向你计算机中存储那个对象的内存. print 'Simple Assignment' sho ...
- 《简明python教程》笔记一
读<简明Python教程>笔记: 本书的官方网站是www.byteofpython.info 安装就不说了,网上很多,这里就记录下我在安装时的问题,首先到python官网下载,选好安装路 ...
- (原+转)简明 Python 教程:总结
简明 Python 教程 说明:本文只是对<简明Python教程>的一个总结.请搜索该书查看真正的教程. 第3章 最初的步骤 1. Python是大小写敏感的. 2. 在#符号右面的内容 ...
- 笔记|《简明Python教程》:编程小白的第一本python入门书
<简明Python教程>这本书是初级的Python入门教材,初级内容基本覆盖,对高级内容没有做深入纠结.适合刚接触Python的新手,行文比较简洁轻松,读起来也比较顺畅. 下面是我根据各个 ...
- 《简明Python教程》学习笔记
<简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Pa ...
- 简明Python教程自学笔记——命令行通讯录
[前言]学习Python已经有一段时间了,相关的书籍资料也下载了不少,但是没有一本完整的看完,也没有编出一个完整的程序.今天下午比较清闲就把<简明Python教程>看了一遍,然后根据书里面 ...
- 【转】简明 Python 教程
原文网址:http://woodpecker.org.cn/abyteofpython_cn/chinese/ 简明 Python 教程Swaroop, C. H. 著沈洁元 译www.byteof ...
- python读书笔记-《简明python教程》上
1月15日 <简明python教程>上 基本结构: 基础概念+控制流+函数+模块+数据结构+面向对象+I/O+异常+标准库+其他 1.概念 1-0 退出python linux: ...
- 学习笔记《简明python教程》
学习笔记<简明python教程> 体会:言简意赅,很适合新手入门 2018年3月14日21:45:59 1.global 语句 在不使用 global 语句的情况下,不可能为一个定义于函数 ...
随机推荐
- 浅谈配置文件:spring-servlet.xml(spring-mvc.xml) 与 applicationContext.xml
在搭建 spring mvc 的框架时,会有2个配置文件必不可少: spring-servlet.xml 和applicationContext.xml.第一次接触spring mvc的工程师可能会对 ...
- linux less使用方法
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- MFC多国语言——资源DLL
此随笔中主要内容来自http://blog.csdn.net/china_hxx/article/details/10066655,原出处不详. 以下内容基于VC 6.0.要实现界面多语言化,必须要先 ...
- point-position目标定位
双站探测同一目标会构成两条直线:(飞行目标定位2 - ostartech - 博客园 https://www.cnblogs.com/wxl845235800/p/8858116.html) 测角偏差 ...
- 你不知道的Console命令
一.显示信息的命令 1: <!DOCTYPE html> 2: <html> 3: <head> 4: <title>常用console命令</t ...
- Cannot call sendRedirect() after the response has been committed错误;
Cannot call sendRedirect() after the response has been committed提示信息其实很清楚,如果response已经提交过了,就无法再发送sen ...
- java人民币转大写中文
代码如下: import java.math.BigDecimal; /** * @author andy * @create 2016-08-12 18:51 */ public class Pri ...
- noip2006 金明的预算
题目链接:传送门 题目大意:略.. 题目思路:其实单就这道题来说,一个主件最多两个附件,且附件不再包含附件,所以很简单,但是如果主件的附件无限制,附件也可包含无限制的附件,应该怎么做? 首先推荐一篇论 ...
- JZOJ.5273【NOIP2017模拟8.14】亲戚
Description
- 【BZOJ2055】80人环游世界 有上下界费用流
[BZOJ2055]80人环游世界 Description 想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么 一个 ...