Size: 1708
Comment: 删除对PageComment2组件的引用
|
← Revision 3 as of 2009-12-25 07:08:58 ⇥
Size: 1708
Comment: converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 84: | Line 84: |
创建 by -- RexChen[[[DateTime(2008-08-04T00:14:20Z)]]] | 创建 by -- RexChen[<<DateTime(2008-08-04T00:14:20Z)>>] |
List
Python中的list,与ruby中的数组、java中的数组(功能上像List)类似。
基本操作
创建
ls = [1, "2", 3]
检索
索引
ls[1]
可以使用负索引
ls[-1]
以上返回地三个元素3,该种方式可以记为ls(len(ls)-1)
可以对list中元素进行分段,所以分段所得的list均为新的list
ls[1:3] ls[1:-1]
以上获取从索引为1的元素开始,一直向右到索引为3或者-1的元素为止,但不包括索引为3或者-1的元素。
ls[:3] ls[3:] ls[:]
第一个返回从0开始,返回前3个元素,第二句返回最后3个元素,第三句返回所有元素,与ls不同的是,第三句返回的是一个ls的copy。
搜索
index 方法检索对应的元素,并返回元素的索引值
idx = ls.index("2")
in 关键字用于判断元素是否存在,存在则返回True,否则返回False
"2" in ls
修改
修改原有元素
ls[0] = 5
新增元素
append 向list末尾新增一个元素
ls.append("new")
insert 向指定位置新增一个元素
ls.insert(2, "new")
extend 将一个list与当前list合并
ls.extend(newls)
删除
remove 仅仅只删除首次出现的记录,如若不存在,则会报错
ls.remove(0)
pop方法删除list中最后一个元素,同时返回该元素
el = ls.pop()
反馈
创建 by -- RexChen[2008-08-04 00:14:20]