习题38 列表的操作
来源:互联网 发布:数据分析师 挂靠 编辑:程序博客网 时间:2024/05/20 05:26
这节课讲了一部分列表的操作,先给源代码
#-*-coding:utf-8-*-ten_things = "Apples Oranges Crows Telephone Light Suger" # Crow 乌鸦print "Wait there's not 10 things in that list, let's fix that."stuff = ten_things.split(' ')more_stuff = ["Day","Night","Song","Frisbee","Corn","Banana","Girl","Boy"] # Frisbee 飞盘while len(stuff) != 10: next_one = more_stuff.pop() print "Adding: ", next_one stuff.append(next_one) print "There's %d items now." % len(stuff)print "There we go: ", stuffprint "Let's do some things with stuff."print stuff[1]print stuff[-1] # whoa! fancyprint stuff.pop()print ' '.join(stuff) # what? coolprint '#'.join(stuff[3:5]) # super stellar!这个代码的运行结果是这样的:
===================================================================================================
附加练习
1-3.
#-*-coding:utf-8-*-ten_things = "Apples Oranges Crows Telephone Light Suger" # Crow 乌鸦print "Wait there's not 10 things in that list, let's fix that."# 这时候列表 ten_things 里面其实没有十个元素stuff = ten_things.split(' ')# 创建一个叫 stuff 的列表,从 ten_things 里面用 split() 切割,以空格作为切割标志# split(ten_things, ' ')# 为 ten_things 和 ' ' 调用 split 函数# Python split()通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串more_stuff = ["Day","Night","Song","Frisbee","Corn","Banana","Girl","Boy"] # Frisbee 飞盘# 给一个叫 more_stuff 的列表,里面有很多另外的东西while len(stuff) != 10: # 如果 stuff 的长度不为10 next_one = more_stuff.pop() # 创建变量 next_one ,从 more_stuff 里面进一个 # 为 more_stuff 调用 pop 函数 # Python list.pop() 方法: 从列表中移除并返回最后一个对象或者obj # 意思是在这里是从最后的 Boy 开始返回的 print "Adding: ", next_one stuff.append(next_one) # append(stuff, nextone) # 为 stuff 和 next_one 调用 append 函数 print "There's %d items now." % len(stuff)print "There we go: ", stuffprint "Let's do some things with stuff."print stuff[1]# 输出 Oranges,这是 stuff 列表的第二个元素print stuff[-1] # whoa! fancy# 输出 Corn,这是 stuff 列表的最后一个元素print stuff.pop()# 输出 Corn,并且移除 Cornprint ' '.join(stuff) # what? cool# join(' ', stuff)# 为 stuff 和 ' ' 调用 join 函数# Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串# 这里就是用空格把新 stuff 里面的元素隔开然后生成一个字符串print '#'.join(stuff[3:5]) # super stellar!# join(' ', stuff[3:5])# 把 # 字符用来隔开第4个和第5个元素
4.
Python 的类 ( Class ) :
类有这样一些的优点
1) 、类对象是多态的:也就是多种形态,这意味着我们可以对不同的类对象使用同样的操作方法,而不需要额外写代码。
2)、类的封装:封装之后,可以直接调用类的对象,来操作内部的一些类方法,不需要让使用者看到代码工作的细节。
3)、类的继承:类可以从其它类或者元类中继承它们的方法,直接使用。
然而还是不懂,以后也许就懂了。
真的这种东西要实际上手才行,光看是看不明白的
0 0
- 习题38 列表的操作
- 列表的操作,习题38,learn python the hard way.
- 笨方法学Python 习题 38: 列表的操作
- SICP 习题 (2.25)解题总结:列表的操作
- 笨方法学习Python-习题39: 列表的操作
- vector操作的习题
- Exercise 38: 列表的操作
- 习题34 访问列表的元素
- 顺序列表的操作
- 列表的一些操作
- 列表的常用操作
- 列表的基本操作
- 列表部分习题
- Python数据类型 -- 列表习题
- 一个P,V操作的习题
- exercise 38 列表操作
- SICP 习题 (2.17)解题总结:列表的遍历
- SICP 习题 (2.24)解题总结:列表的结构
- Java内存管理
- AC自动机 ( 静态建树模板 )——Substring ( UVA 11468 )
- ajax基础 GET,POST方式传值,同步异步
- Ubuntu下的文件比较工具--meld
- pip 换源小计
- 习题38 列表的操作
- 认识tomcat及其配置文件解释
- 从一个包含汉字和其他字符的字符串中截取指定字节长度的字符串,不可以出现中文乱码
- HDU 1258 Sum It Up
- spring的一些配置问题
- Qt中的MVC (模型/视图结构)
- POJ 2481 Cows
- POJ 1873 The Fortified Forest(二进制枚举+凸包)
- nginx的权限问题(13: Permission denied) 解决办法