标签:
1. 向列表的尾部添加一个新的元素
append(...)
L.append(object) -- append object to end
|
1
2
3
4
|
>>> a = [‘sam‘,24,‘shaw‘]>>> a.append(‘35‘)>>> a[‘sam‘, 24, ‘shaw‘, ‘35‘] |
2. 查找list中有多少个value
count(...)
L.count(value) -> integer -- returnnumber of occurrences of value
|
1
2
3
|
>>> L = [12,‘school‘,‘ball‘,24,12]>>> L.count(12)2 |
3. 用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
extend(...)
|
1
2
3
4
5
|
>>> L =[12,‘school‘,‘ball‘,24,12]>>> S =[‘haha‘,25,‘mail‘]>>> L.extend(S)>>> L[12, ‘school‘, ‘ball‘, 24, 12,‘haha‘, 25, ‘mail‘] |
4. 用于将指定对象插入列表
insert(index,object)
|
1
2
3
4
|
>>> L =[12,‘school‘,12]>>>L.insert(0,‘shaw‘)>>> L[‘shaw‘, 12, ‘school‘, 12] |
5. 用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
pop(...)
说明:
L.pop([index]) -> item -- remove andreturn item at index (default last). RaisesIndexError if list is empty or index is out of range.
|
1
2
3
4
5
|
>>> L = [‘shaw‘, 12,‘school‘, 12]>>> L.pop() #(默认删除最后一个)12>>> L.pop(0)‘shaw‘ #(删除第一个) |
6. 检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在string中会报一个异常
str.index(str, beg=0, end=len(string))
参数:
str -- 指定检索的字符串
beg -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度。
|
1
2
3
4
5
6
7
|
>>> L = [‘shaw‘, 12,‘school‘, 12]>>> L.index(‘shaw‘)0>>> L.index(‘sam‘)Traceback (most recent calllast): File "<input>", line 1, in<module>ValueError: ‘sam‘ isnot in list |
7. 用于移除列表中某个值的第一个匹配到的元素。
L.remove(value)
Raises ValueError if the value is not present.
|
1
2
3
4
5
6
7
|
>>> L = [‘shaw‘, 12,‘school‘, 12]>>> L.remove(12)>>> L[‘shaw‘, ‘school‘, 12]>>> L.remove(12)>>> L[‘shaw‘, ‘school‘] |
8. 用于反向列表中元素(对列表的元素进行反向排序)
reverse(...)
|
1
2
3
4
|
>>> L = [‘shaw‘, 12,‘school‘]>>> L.reverse()>>> L[‘school‘, 12, ‘shaw‘] |
9. 用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。(把list中value排序(先数字,在大写字母,小写字母))
L.sort(cmp=None, key=None, reverse=False)
|
1
2
3
4
|
>>> L = [‘Shaw‘, 12,‘abc‘,24,‘biu‘,‘cd‘]>>> L.sort()>>> L[12, 24, ‘Shaw‘, ‘abc‘, ‘biu‘,‘cd‘] |
标签:
原文地址:http://www.cnblogs.com/opsedu/p/5497782.html