Learn Python the Hard Way(“笨办法”学python) 第四版 例39笔记

来源:互联网 发布:java泛型坑 编辑:程序博客网 时间:2024/06/05 03:07

39 列表的操作
python菜鸟最近在学Learn Python the Hard Way这本书,将笔记分享出来和大家一起学习!
小点解析(来自脚本之家和陶子自己的理解):
**1.**str.split(str=”“,num=string.count(str))
str——分隔符,默认为所有的空字符,包括空格,换行(\n), 制表符(\t)等,上面的例子中用的是空格
num——分割次数
**2.**list.pop(obj=list[])
list.pop()默认移除最后一个元素并返回该元素值
**3.**str.join(sequence)
注意理解join(str,sequence)
将sequence中的元素以str连接

ten_things="Apple Oranges Crows Telephone Light Sugar"print("Wait there's not 10 things in that list,lest's fix that.")stuff=ten_things.split(' ')#此处指定分隔符为空格,经过分割之后,len(stuff)等于6more_stuff['Day','Night','Song','Firsbee','Corn','Banana','Girl','Boy']while len(stuff)!=10:    next_one=more_stuff.pop() #指的是删除more_stuff最后     一个元素并将该元素返回,赋值给else    print("Adding:",next_one)    stuff.append(next_one)    print("There's %d items now."%len(stuff))print("There we go:",stuff)print("Let's do some things with stuff")print(stuff[1])#打印第一个元素print(stuff[-1])#打印最后一个元素print(stuff.pop())print(' '.join(stuff))print('#'.join(stuff[3:5])) #将stuff中的3和4位置上元素以#连接
阅读全文
0 0
原创粉丝点击