列表(list):
说明:列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型。创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。
示例:
name_list1 = ['physics', 'chemistry', 1997, 2000]
name_list2 = [1, 2, 3, 4, 5 ]
name_list3 = ["a", "b", "c", "d"]
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
class list(object): """ list() -> new empty list list(iterable) -> new list initialized from iterable's items """ def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -> None -- append object to end """ pass def clear(self): # real signature unknown; restored from __doc__ """ L.clear() -> None -- remove all items from L """ pass def copy(self): # real signature unknown; restored from __doc__ """ L.copy() -> list -- a shallow copy of L """ return [] def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass def pop(self, index=None): # real signature unknown; restored from __doc__ """ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. """ pass def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ pass def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ Return key in self. """ pass def __delitem__(self, *args, **kwargs): # real signature unknown """ Delete self[key]. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __iadd__(self, *args, **kwargs): # real signature unknown """ Implement self+=value. """ pass def __imul__(self, *args, **kwargs): # real signature unknown """ Implement self*=value. """ pass def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable's items # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self
常用功能:
索引示例:
name_list = ['one','two','three']print(name_list[0])print(name_list[1])print(name_list[2])运行结果:onetwothree
索引删除示例:
name_list = ['one','two','three']del name_list[1]print(name_list)运行结果:['one', 'three']
切片示例:
name_list = ['one','two','three']print(name_list[0:2])print(name_list[2:len(name_list)])运行结果:['one', 'two']['three']
切片删除示例:
name_list = ['one','two','three']del name_list[1:3]print(name_list)运行结果:['one']
for循环示例:
name_list = ['one','two','three']for i in name_list: print(i)运行结果:onetwothree
列表内部提供的其他功能:
append(self, p_object):
说明:用于在列表末尾添加新的对象。p_object:追加的对象。(返回None)
示例:
name_list1= ['one','two','three']name_list2= ['one','two','three']name_list3= ['one','two','three']name_list1.append('four')name_list2.append(['1','2','3'])name_list3.append(4)print(name_list1)print(name_list2)print(name_list3)运行结果:['one', 'two', 'three', 'four']['one', 'two', 'three', ['1', '2', '3']]['one', 'two', 'three', 4]
- 列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。
- 只向列表的尾部添加一个新的元素。
count(self,value):
说明:统计列表中某个对象出现的次数。value:要统计的对象。(返回int)
示例:
name_list1= ['one','two','three']name_list1.append('four')name_list1.append('four')name_list1.append('four')print(name_list1)print(name_list1.count('four'))运行结果:['one', 'two', 'three', 'four', 'four', 'four']3
extend(self, iterable):
说明:在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。iterable(可迭代的):扩展的对象(返回None)
示例:
aList = [123, 'xyz', 'zara', 'abc', 123]bList = [2009, 'manni']aList.extend(bList)print ("Extended List : ", aList)运行结果:Extended List : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
index(self, value, start=None, end=None):
说明:从列表中找出某个值第一个匹配项的索引位置。value:查找匹配项。start:开始位置(从0开始),默认为无。end:结束位置,默认为无。(返回int)
示例:
aList = [123, 'xyz', 'zara', 'abc', 123]print(aList.index(123))运行结果:0
insert(self, index, p_object):
说明:用于将指定对象插入列表的指定位置。index:位置。p_object:插入的对象。
示例:
aList = [123, 'xyz', 'zara', 'abc', 123]aList.insert(1,'SB')print(aList)运行结果:[123, 'SB', 'xyz', 'zara', 'abc', 123]
pop(self, index=None):
说明:移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。index:移除元素的位置,默认为无。(返回移除的元素的值)
示例:
aList = [123, 'xyz', 'zara', 'abc', 123]aList.pop(2)print(aList)运行结果:[123, 'xyz', 'abc', 123]
remove(self, value):
说明:移除列表中某个值的第一个匹配项。value:移除的第一个匹配元素。(返回None)
示例:
aList = [123, 'xyz', 'zara', 'abc', 123]aList.remove('xyz')print(aList)运行结果:[123, 'zara', 'abc', 123]
reverse(self):
说明:反向列表中元素。
示例:
aList = [123, 'xyz', 'zara', 'abc', 567]aList.reverse()print(aList)运行结果:[567, 'abc', 'zara', 'xyz', 123]
sort(self, key=None, reverse=Flase):
说明:对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
示例:
aList = [ 'xyz', 'zara', 'abc', 'xyz']aList.sort()print ("List : ", aList)运行结果:List : ['abc', 'xyz', 'xyz', 'zara']