第七章、 数据结构_第1页
免费预览已结束,剩余3页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第七章、 数据结构python中有三种内建的数据结构 列表、元组和字典。 1) lists列表 , 列表是序列的一种 shoplist = 'apple', 'carrot', 'banana' print shoplist 'apple', 'carrot', 'banana' shoplist.append('orange') 末尾加入一个 print shoplist 'apple', 'carrot', 'banana', &#

2、39;orange' shoplist.insert(2, 'flint') 指定位置插入 print shoplist 'apple', 'carrot', 'flint', 'banana', 'orange' shoplist.reverse() 反转 print shoplist 'orange', 'banana', 'flint', 'carrot', 'apple' shoplist.pop() 默

3、认从末尾删除 print shoplist 'orange', 'banana', 'flint', 'carrot' shoplist.() 正向排序 print shoplist 'banana', 'carrot', 'flint', 'orange' del shoplist1 指定位置删除,等于shoplist.pop(1) print shoplist 'banana', 'flint', 'orange'

4、shoplist.tend('database', 'egg') 末尾加入多个 print shoplist 'banana', 'flint', 'orange', 'database', 'egg' shoplist.remove('banana') 删除指定项 print shoplist 'flint', 'orange', 'database', 'egg' 通过help(list)获得完整的学

5、问。 列表解析表达式 从一个已有的列表导出一个新的列表。 vec = 2, 4, 6 print (3 * x for x in vec) generator object gen at 0x00e7d300 print list(3 * x for x in vec) 6, 12, 18 print 3 * x for x in vec 6, 12, 18 print 3 * x for x in vec if x 3 12, 18 print 3 * x for x in vec if x 2 print x, x*2 for x in vec 2, 4, 4, 16, 6, 36 m =

6、 1, 2, 3, 4, 5, 6, 7, 8, 9 print row1 for row in m 2, 5, 8 print row1 for row in m if row1 % 2 = 0 2, 8 print x + y for x in 'abc' for y in 'mn' 'am', 'an', 'bm', 'bn', 'cm', 'cn' 处理大型矩阵用法开源numpy系统 nesting嵌套 m = 1, 2, 3, 4, 5, 6, 7, 8,

7、9 print m 1, 2, 3, 4, 5, 6, 7, 8, 9 print m0 1, 2, 3 print m12 6 列表解析 list(map(m, m) 6, 15, 24 sum(row) for row in m (24, 6, 15) x: ord(x) for x in 'spabm' 'a': 97, 'p': 112, 's': 115, 'b': 98, 'm': 109 索引操作符 索引操作符,下标操作,从0开头,支持反向索引,从-1开头 sp = 'a

8、9;, 'b', 'c', 'd' sp0, sp1, sp2, sp3 ('a', 'b', 'c', 'd') sp-4, sp-3, sp-2, sp-1 ('a', 'b', 'c', 'd') name = 'swaroop' len(name) 7 name-1 'p' namelen(name) - 1 'p' 切片操作符xi:j 切片(slice)操作符,序

9、列名跟方括号,方括号中有一对可选的数字,并用冒号分割。数是可选的,而冒号是必需的。 xi:j,取出在x中从偏移为i,直到但不包括j的内容 shoplist = 'a', 'b', 'c', 'd' shoplist1:3 'b', 'c' shoplist2: 'c', 'd' shoplist1:-1 'b', 'c' shoplist: 'a', 'b', 'c', 'd

10、9; name = 'swaroop' name1:3 wa,不包括r! name2: aroop name1:-1 waroo name: swaroop name * 2 swaroopswaroop 三元切片操作符xi:j:k xi:j = xi:j:1 s = "abefghijklmn" print s1:10:2 bhj print s:2 acegikm print s:-2 nljhfdb 参考 shoplist = 'apple', 'mango', 'carrot', 'banana

11、' mylist = shoplist mylist is just another name pointing to the same object! del shoplist0 print shoplist 'mango', 'carrot', 'banana' print mylist 'mango', 'carrot', 'banana' mylist = shoplist: make a copy by doing a full slice del mylist0 remove f

12、irst item print shoplist 'mango', 'carrot', 'banana' print mylist 'carrot', 'banana' 假如要复制列表或者序列或者对象,那么你必需用法切片操作符来取得拷贝。记住列表的赋值语句不创建拷贝 浅拷贝深拷贝 浅拷贝(1)彻低切片操作:;(2)利用工厂函数,如list(),dict();(3)用法copy模块的copy函数 深拷贝(1)用法copy模块的deepcopy()函数 模拟堆栈 stack = def pushit(): stack.

13、append(raw_input("enter new sing:").strip() def popit(): if len(stack) = 0: print 'can not pop from an empty stack!' ee: print 'remov ', stack-1, '' stack.pop() def viewstack(): print stack cmds = 'u':pushit, 'o':popit, 'v':viewstack def show

14、menu(): pr = """p(u)sh p(o)p (v)iew (q)uit enter choice:""" while true: while true: try: choice = raw_input(pr).strip()0.lower() except (eoferror, keyboardinterrupt, indexerror): choice = 'q' print 'nyou picked: %s' %choice if choice not in 'uovq'

15、;: print 'inval option, try again' else: break if choice = 'q': break cmdschoice() if _name_ = '_main_': showmenu() 2) tuples元组(,) 元组是不行变的 即不能修改。元组通过圆括号中用逗号分割的项目定义。 zoo = ('w', 'e', 'p') new_zoo = ('m', 'd', zoo) print zoo ('w',

16、 'e', 'p') print new_zoo ('m', 'd', ('w', 'e', 'p') print new_zoo2 ('w', 'e', 'p') print new_zoo22 p new_zoo1 = 'x' typeerror: 'tuple' object does not support item assignment 元组最通常的使用是用在打印语句中 age = 22 name = 'swaroop' print '%s is %d years old' % (name, age) print 'why is %s playing with that python?' % name print语句用法跟着%符号的项目元组的字符串。定制输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。 3) dictionaries字典k:v 键值对:d = key1 : value1, key2 : value2 rec

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论