代码语言:javascript复制
Python list类常用方法
class list(object):
def append(self, p_object): # 向列表中添加元素;
>>> name_list
['shuoming', 'python', 'search']
>>> name_list.append("python")
>>> name_list
['shuoming', 'python', 'search', 'python']
def clear(self): # 清除列表所有元素,清除后列表为空列表;
>>> test_list
['shuoming', 'python', 'search', 'python', 'automatic', 1, 2]
>>> test_list.clear()
>>> test_list
[]
>>>
def copy(self):
def count(self, value): # 列表中指定元素的个数;
>>> name_list.count("python")
2
def extend(self, iterable): 把一个列表添加到另一个列表中;
>>> a
[1, 7, 2, 9, 3, 8, 'a', 'b']
>>> b=[2,'f',3]
>>> a b
[1, 7, 2, 9, 3, 8, 'a', 'b', 2, 'f', 3]
>>> a.extend(b)
>>> a
[1, 7, 2, 9, 3, 8, 'a', 'b', 2, 'f', 3]
>>>
def index(self, value, start=None, stop=None): # 查列表中某一元素第一个索引值;
>>> name_list.index("python")
1
def insert(self, index, p_object): # 向列表中插入某一元素;
>>> name_list.insert(3,"python")
>>> name_list
['shuoming', 'python', 'search', 'python', 'python']
def pop(self, index=None): # 从列表最后删除元素;
>>> last_one = name_list.pop()
>>> last_one
'python'
>>> name_list
['shuoming', 'python', 'search', 'python']
def remove(self, value): # 删除某一元素,默认是指定元素的第一个值;
>>> name_list.remove('python')
>>> name_list
['shuoming', 'search', 'python']
def reverse(self): # 翻转;
>>> name_list
['shuoming', 'search', 'python', '@', '$']
>>> name_list.reverse()
>>> name_list
['$', '@', 'python', 'search', 'shuoming']
def sort(self, key=None, reverse=False): # 排序,特殊字符在前;
>>> name_list
['shuoming', 'search', 'python', '@', '$']
>>> name_list.sort()
>>> print (name_list)
['$', '@', 'python', 'search', 'shuoming']