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教程五----数据结构的更多相关文章

  1. 简明python教程五----数据结构(下)

    引用 当你创建一个对象并给它赋一个变量的时候,这个变量仅仅引用那个对象,而不是表示这个对象本身!即,变量名指向你计算机中存储那个对象的内存. print 'Simple Assignment' sho ...

  2. 《简明python教程》笔记一

    读<简明Python教程>笔记: 本书的官方网站是www.byteofpython.info  安装就不说了,网上很多,这里就记录下我在安装时的问题,首先到python官网下载,选好安装路 ...

  3. (原+转)简明 Python 教程:总结

     简明 Python 教程 说明:本文只是对<简明Python教程>的一个总结.请搜索该书查看真正的教程. 第3章 最初的步骤 1. Python是大小写敏感的. 2. 在#符号右面的内容 ...

  4. 笔记|《简明Python教程》:编程小白的第一本python入门书

    <简明Python教程>这本书是初级的Python入门教材,初级内容基本覆盖,对高级内容没有做深入纠结.适合刚接触Python的新手,行文比较简洁轻松,读起来也比较顺畅. 下面是我根据各个 ...

  5. 《简明Python教程》学习笔记

    <简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Pa ...

  6. 简明Python教程自学笔记——命令行通讯录

    [前言]学习Python已经有一段时间了,相关的书籍资料也下载了不少,但是没有一本完整的看完,也没有编出一个完整的程序.今天下午比较清闲就把<简明Python教程>看了一遍,然后根据书里面 ...

  7. 【转】简明 Python 教程

    原文网址:http://woodpecker.org.cn/abyteofpython_cn/chinese/ 简明 Python 教程Swaroop, C. H. 著沈洁元  译www.byteof ...

  8. python读书笔记-《简明python教程》上

    1月15日 <简明python教程>上 基本结构: 基础概念+控制流+函数+模块+数据结构+面向对象+I/O+异常+标准库+其他   1.概念 1-0    退出python linux: ...

  9. 学习笔记《简明python教程》

    学习笔记<简明python教程> 体会:言简意赅,很适合新手入门 2018年3月14日21:45:59 1.global 语句 在不使用 global 语句的情况下,不可能为一个定义于函数 ...

随机推荐

  1. 在Linux下搭建git服务器

    http://www.cnblogs.com/dee0912/p/5815267.html 步骤很详细,很受用

  2. Python中集合类型(set)学习小结

    set 是一个无序的元素集合,支持并.交.差及对称差等数学运算, 但由于 set 不记录元素位置,因此不支持索引.分片等类序列的操作. 初始化 复制代码代码如下: s0 = set()d0 = {}s ...

  3. UVA1349 Optimal Bus Route Design 拆点法+最小费用最佳匹配

    /** 题目:UVA1349 Optimal Bus Route Design 链接:https://vjudge.net/problem/UVA-1349 题意:lrj入门经典P375 给n个点(n ...

  4. Hive学习笔记——HQL用法及UDF,Transform

    Hive中没有定义专门的数据格式,数据格式可以由用户指定,用户定义数据格式需要指定三个属性:列分隔符(通常为空格.”\t”.”\x001″).行分隔符 (”\n”)以及读取文件数据的方法(Hive 中 ...

  5. 第二百二十七节,jQuery EasyUI,ComboTree(树型下拉框)组件

    jQuery EasyUI,ComboTree(树型下拉框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解EasyUI中ComboTree(树型下拉框)组件的使用方法,这个 ...

  6. Log4j 汇总

    一.概念 .1. log4j是 是线程安全的 日志框架,高度可配置,可通过在运行时的外部文件配置. 默认情况下,日志管理在CLASSPATH 查找一个名为 log4j.properties 的文件. ...

  7. mybatis 处理 mysql 表中的 text类型的 字段

    在mysql 中 text类型的字段: service_detail text NULL 服务描述   . 对应java文件中 model 中的 String:  private String ser ...

  8. layoutSubviews何时调用的问题

    本文转载至 http://www.cnblogs.com/pengyingh/articles/2417211.html 今天跟旺才兄学习了一下UIView的setNeedsDisplay和setNe ...

  9. html5 canvas 详细使用教程 转

     分类: html5(9)  原文地址:http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html 原作很强悍 导航 前言 基本知识 绘 ...

  10. SQL转换全角和半角函数

    SQL转换全角和半角函数 CREATE FUNCTION f_Convert( ), --要转换的字符串 @flag bit --转换标志,0转换成半角,1转换成全角 )) AS BEGIN ),@s ...