Learn Python the hard way, ex39 列表的操作
2024-09-05 04:05:08
#!/usr/bin/python
#coding:utf-8 ten_things = "apples oranges crows telephone light sugar"
print "wait there's not 10 things in that list,let's fix that" stuff = ten_things.split(' ') #10个东西,以' '间隔开
more_stuff = ["day","night","song","frisbee","corn","banana","girl","boy"] #更多的原料 while len(stuff) != 10: #取得List stuff的长度,当不等于10时循环
next_one = more_stuff.pop() #截取more_stuff的最后一个元素(默认值)
print "adding:",next_one #输出pop取得的值
stuff.append(next_one) #使用append将之前截取的值赋予list:stuff
print "there's %d items now" % len(stuff) print "there we go : ",stuff print "let's do some things with stuff" print stuff[1] #第二个元素,list从0计数
print stuff[-1] #whoa!fancy 最后一个元素(-1)
print stuff.pop()
print ' '.join(stuff) #what?cool!
print '#'.join(stuff[3:5]) # super stellar
Output:
wait there's not 10 things in that list,let's fix that
adding: boy
there's 7 items now
adding: girl
there's 8 items now
adding: banana
there's 9 items now
adding: corn
there's 10 items now
there we go : ['apples', 'oranges', 'crows', 'telephone', 'light', 'sugar', 'boy', 'girl', 'banana', 'corn']
let's do some things with stuff
oranges
corn
corn
apples oranges crows telephone light sugar boy girl banana
telephone#light
加分题的一些参考: Python 面向对象编程 函数式编程
最新文章
- Centos7 wifi
- MYSQL开启慢查询日志实施
- ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)
- window.location.href url含中文服务器收到乱码问题解决
- 一步一步搭建客服系统 (2) 如何搭建SimpleWebRTC信令服务器
- OpenXml入门----给Word文档添加表格
- depth_write
- 让qq图标在自己的网站上显示方法
- C++编程规范之23:头文件应该自给自足
- HDU 1398 Square Coins
- 高效程序猿之 VS2010快速生成代码模板
- Oracle数据泵(上)
- Codeforces Round #300(Div. 2)-538A.str.substr 538B.不会 538C.不会 。。。
- Centos中安装gitlab
- [转]TopShelf 用法
- Python 高度定制化自己的线程类和进程类代码,获取启动进程或线程方法的结果(兼容Py2和Py3)
- mysql 开发基础系列16 视图
- iOS开发简记(8):数据持久化
- 研究一下Spark Hash Shuffle 和 SortShuffle 原理机制
- SpringBoot的json序列化及时间序列化处理