python之列表对象
1. 获取列表中的某个值
描述:获取下标所对应的值
语法:
print(li[0]) #[取索引值]
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
li = list(['a','b','c']) val=(li[0]) #获取下标所对应的值 print(val) a #显示结果
2. append
描述:追加对象到列表中
语法:
def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -> None -- append object to end """ pass
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
li = list(['a','b','c']) li.append('d') #追加d到列表中 print(li) ['a', 'b', 'c', 'd'] #显示结果
3. insert
描述:插入对象到列表中
语法:
def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [1,2,3,4] list.insert(4,'a') #第4个索引前插入a print(list) [1, 2, 3, 4, 'a'] #显示结果
4. pop
描述:删除队列中最后一个对象
语法:
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
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [1,2,3,4] list.pop() #默认删除队列中最后一个对象 print(list) [1, 2, 3] #显示结果
5. remove
描述:删除队列中指定的对象
语法:
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
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [1,2,3,4] list.remove(3) #删除队列中的3 print(list) [1, 2, 4] #显示结果
6. sort
描述:队列排序
语法:
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
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [1,2,44,22,3,4] list.sort() #顺序排序显示 print(list) [1, 2, 3, 4, 22, 44] #显示结果
7. reverse
描述:队列反转
语法:
def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [1,2,3,4] list.reverse() #队列反转 print(list) [4, 3, 2, 1] #显示结果
8. index
描述:返回队列中值所对应的下标值,仅限第一匹配
语法:
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
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [11,2,33,3,4,11,5,33] print(list.index(11)) #查找队列中值为11的对象 0 #显示结果,返回11所在的下标值
9. count
描述:计算队列中对象的个数
语法:
def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [11,2,33,3,4,11,5,33] print(list.count(33)) #查找对象为33的个数,匹配所有对象 2 #显示结果
10. extend
描述:对列表进行扩展,可添加其他列表或者元组
语法:
def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ pass
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
li = list([1,2,3]) li.extend([11,22, ]) #扩展添加队列到列表中 print(li) [1, 2, 3, 11, 22] #显示结果 li = list([1,2,3]) li.extend((11,22, )) #扩展添加元组到列表中 print(li) [1, 2, 3, 11, 22] #显示结果
11. clear
描述:清楚列表数据
语法:
def clear(self): # real signature unknown; restored from __doc__ """ L.clear() -> None -- remove all items from L """ pass
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
li = list([1,2,3]) li.clear() #清楚列表中内容 print(li) [] #显示结果
12. 切片
描述:对列表中数据进行切片显示
语法:
[start:stop:step]
[开始索引:结束索引:步长值]
开始索引:同其它语言一样,从0开始。序列从左向右方向中,第一个值的索引为0,最后一个为-1
结束索引:切片操作符将取到该索引为止,不包含该索引的值。
步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作。步长值为正时表示从左向右取,如果为负,则表示从右向左取。步长值不能为0
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
list = [1,2,3,4] print (list[:-1]) #默认开始索引为0,结束索引为队列最后一个索引值 [1, 2, 3] #显示结果 list = [1,2,3,4] print (list[2:4]) #取开始索引2,结束索引4的内容 [3,4] #显示结果 list = [1,2,3,4] print (list[::-1]) #反向取值 [4, 3, 2, 1] #显示结果