免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2251 | 回复: 0
打印 上一主题 下一主题

python学习笔记--list [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-03 22:44 |只看该作者 |倒序浏览
3.2.2. 向 list 中增加元素
例 3.10. 向 list 中增加元素
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li.append("new")               
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/1.png
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")            
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/2.png
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
>>> li.extend(["two", "elements"])
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/3.png
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/1.png

append 向 list 的末尾追加单个元素。
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/2.png

insert 将单个元素插入到 list 中。数值参数是插入点的索引。请注意, list 中的元素不必唯一, 现在有两个独立的元素具有 'new' 这个值, li[2] 和 li[6]。
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/3.png

extend 用来连接 list。请注意不要使用多个参数来调用 extend, 要使用一个 list 参数进行调用。在本例中, 这个 list 有两个元素。
例 3.11. extend (扩展) 与 append (追加)的差别
>>> li = ['a', 'b', 'c']
>>> li.extend(['d', 'e', 'f'])
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/1.png
>>> li
['a', 'b', 'c', 'd', 'e', 'f']
>>> len(li)                    
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/2.png
6
>>> li[-1]
'f'
>>> li = ['a', 'b', 'c']
>>> li.append(['d', 'e', 'f'])
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/3.png
>>> li
['a', 'b', 'c', ['d', 'e', 'f']]
>>> len(li)                    
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/4.png
4
>>> li[-1]
['d', 'e', 'f']
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/1.png

Lists 的两个方法 extend 和 append 看起来类似, 但实际上完全不同。  extend 接受一个参数, 这个参数总是一个 list, 并且添加这个 list 中的每个元素到原 list 中。
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/2.png

在这里 list 中有 3 个元素 ('a', 'b' 和 'c'), 并且使用另一个有 3 个元素 ('d', 'e' 和 'f') 的 list 扩展之, 因此新的 list 中有 6 个元素。
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/3.png

另一方面, append 接受一个参数, 这个参数可以是任何数据类型, 并且简单地追加到 list 的尾部。  在这里使用一个含有 3 个元素的 list 参数调用 append 方法。
file:///home/wangyao/Desktop/diveintopython/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/4.png

原来包含 3 个元素的 list 现在包含 4 个元素。 为什么是 4 个元素呢?  因为刚刚追加的最后一个元素 本身是个 list。  List 可以包含任何类型的数据, 也包括其他的 list。  这或许是您所要的结果, 或许不是。 如果您的意图是  extend, 请不要使用 append。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/12592/showart_136056.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP