数据结构

1. 列表

  • 例子:
#!/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:', # Notice the comma at end of the line
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 I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist
  • 结果
$ python using_list.py
I have 4 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']
  • 列表中可以添加任意种类的对象

  • for in实现了列表的遍历

  • append方法可以添加新的对象

  • sort方法可以对列表排序,是对列表本身的修改

  • del方法后面接上列表中的任意位置,可以删除该对象

2. 元组

  • 元组和列表的形式差不多,不过元组中的对象是不允许修改的

  • 含有0或1个对象的元组比较特殊:一个空的元组由一对空的圆括号组成,如myempty = ()。然而,含有单个元素的元组就不那么简单了。你必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组和表达式中一个带圆括号的对象。即如果你想要的是一个包含项目2的元组的时候,你应该指明singleton = (2 , )。

  • 元组通常用于打印语句:

  • 例子:

#!/usr/bin/python
# Filename: print_tuple.py age = 22
name = 'Swaroop' print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
$ python print_tuple.py
Swaroop is 22 years old
Why is Swaroop playing with that python?
  • 与c中的打印语句类似,不过在python中不用逗号分隔,直接用%定制

  • 在第二个print语句中,我们使用了一个定制,后面跟着%符号后的单个项目——没有圆括号。这只在字符串中只有一个定制的时候有效。

3. 字典

  • 字典其实就是key-value对

  • 例子:

#!/usr/bin/python
# Filename: using_dict.py # 'ab' is short for 'a'ddress'b'ook ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
'Larry' : 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
} print "Swaroop's address is %s" % ab['Swaroop'] # Adding a key/value pair
ab['Guido'] = 'guido@python.org' # Deleting a key/value pair
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: # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']
  • 结果:
$ python using_dict.py
Swaroop's address is swaroopch@byteofpython.info There are 4 contacts in the address-book Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org Guido's address is guido@python.org
  • items方法用于返回一个元组的列表,每个列表中都有键和值,如上面的for循环遍历

  • in方法或者has_key方法可以检验这个键是否存在

序列

上面列表,元组,字典都是序列,都有序列的各种方法,序列就是包括索引操作和切片操作;

例子:

#!/usr/bin/python
# Filename: seq.py shoplist = ['apple', 'mango', 'carrot', 'banana'] # Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2] # Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:] # Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
  • 索引不必多说,下标从0开始,-1表示从右向左数1个,(其实我记忆的话,序列的长度加上这个值,就是他的真实下标)

  • 切片符号':'为标志,包括第一个操作数,不包括第二个,也就是左闭右开

  • 左操作数为空,从开始位置开始,右操作数为空,到结束位置结束

参考

下面是个例子:

#!/usr/bin/python
# Filename: reference.py print 'Simple Assignment'
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object! del shoplist[0] print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object print 'Copy by making a full slice'
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that now the two lists are different
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']

从上面的注释中我们已经能看出来,直接用=连接的赋值相当于直接拷贝的地址,原来序列改变,相应赋值的序列也会改变,但是赋值的时候用序列的切片操作赋值,改变原序列后不会影响到赋值的序列

python学习之路-第五天-python的数据结构的更多相关文章

  1. python学习之路 第五天

    1.装饰器: #!/usr/bin/env python3 user_status = False #用户登录了就把这个改成True def login(auth_type): #把要执行的模块从这里 ...

  2. Python学习之路 (五)爬虫(四)正则表示式爬去名言网

    爬虫的四个主要步骤 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的方式存储和使用) 什么是正则表达式 ...

  3. Python高手之路【五】python基础之正则表达式

    下图列出了Python支持的正则表达式元字符和语法: 字符点:匹配任意一个字符 import re st = 'python' result = re.findall('p.t',st) print( ...

  4. Python 学习笔记(十五)Python类拓展(二)方法

    方法 绑定方法和非绑定方法 绑定方法和非绑定方法在创建时没有任何区别,同一方法,既可以为绑定方法,也可以为非绑定方法,一切不同都只在调用时的手法上有所区别. 绑定方法即该方法绑定类的一个实例上,必须将 ...

  5. Python 学习笔记(十五)Python类拓展(一)继承

    继承 继承(Inheritance):是面向对象软件技术当中的一个概念.如果一个类别A "继承自" 另一个类B,就把这个A称为“B的子类”,而把B称为“A的父类”,也可以称“B是A ...

  6. python学习之路-第七天-python面向对象编程简介

    面向对象编程 在python中,同样是类和对象作为重要的组成部分. 而且在python中基本数据类型如int都是有封装类的,都有自己的方法,应该是和java里面的Integer类似吧 类包括域和方法: ...

  7. python学习之路-第一天-接触python

    我的入门就决定用<简明Python教程> <简明Python教程> 1. python的优势 简单:专注于解决问题而不是关注语言本身 易学:容易上手 开源.免费 可移植性非常强 ...

  8. python学习笔记(十五)python操作数据库

    1.连接mysql,ip,端口号,密码,账号,数据库 2.建立游标 3.执行sql 4.获取结果 5.关闭连接,关闭游标 游标打开仓库的大门: import pymysql conn=pymysql. ...

  9. Python学习(二十五)—— Python连接MySql数据库

    转载自http://www.cnblogs.com/liwenzhou/p/8032238.html 一.Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MyS ...

随机推荐

  1. 玩黑客学校CTF

    关卡很简单就不细说.不懂的百度 第一关: 源码: 解密得出为JO 第二关: 很明白,表单问题. </script> <div style = "text-align: ce ...

  2. file相关的操作,(md5,word转html,复制,删除等)

    package cn.edu.hbcf.common.utils; import java.io.BufferedOutputStream; import java.io.ByteArrayOutpu ...

  3. cubieboard 资源

    开发链接: http://pan.baidu.com/s/1o6r27V0 密码: 46h2 文档资料: http://pan.baidu.com/s/1kTFXMSZ 密码: 9pka 视频资料: ...

  4. Apple设备中point,磅(pt),pixel的关系与转换,以及iPhone模拟器与真机的长度关系

    查阅了好多资料都没有发现有相关的详细介绍,包括苹果官方文档,也是草草带过.后来是在一个介绍Macbook打印字体的博客中看到的,受到启发. 首先说明苹果设备绘图的长度单位可以认为是point,不是磅( ...

  5. 【vijos】1286 座位安排(状压dp)

    https://vijos.org/p/1286 这题好神不会捉... 首先我们知道所有情况有C(n*m, k)种,这个好搞.但是两两不相邻这个有点难搞.. 原来是状压dp..sigh. 设状态f[i ...

  6. ParseChat应用源代码ios版

    ParseChat是一个全然原生的iPhone应用程序.用于创建实时的.基于文本的Parse聊天室.功能:支持多台设备之间的实时聊天,可动态加入新的聊天室,支持基本配置,可发送和接收音效以及随意大小的 ...

  7. abp项目如何按业务功能模块横向分割?

    1.为什么要做分层? 请先看项目分层的最后结构: 2.设计的初衷 项目较大,且主题鲜明,分为:BI模块.DM-用户洞察模块.Manage-应用管理模块.Report应用分析报表模块,大模块之间并无依赖 ...

  8. Hadoop2的HA安装(high availability):JournalNode+ zookeeper

    前面介绍过使用NFS+zookeeper来解决namenode单点失败问题,因为NFS可能也会存在单点问题,所以hadoop提供了一种叫做JournalNode技术,这项技术可以在JournalNod ...

  9. (转)Unity笔记之编辑器(UnityEditor)

    在使用unity3d的过程中,时常会需要从场景中寻找或者调用一个对象,而Unity就提供了一个贴心的功能——拖拽.用鼠标拖一下中比写堆代码直观的多吧!但是Unity提供的远远不止这一丢丢,下面我们来简 ...

  10. SurvivalShooter学习笔记(一.相机跟随)

    1.场景碰撞已好,地板需建一Quad去掉渲染留下碰撞,设置layer为Floor:用于建立摄像机朝向地面的射线,确定鼠标停留点,确定主角需要的朝向. 2.设置摄像机跟随主角: 本例中摄像机设置为正交模 ...