自学Python2.2-基本数据类型-列表list(object)
Python List方法总结
一、 列表简介:
列表是序列对象,可包含任意的Python数据信息,如字符串、数字、列表、元组等
列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作
可以通过list(seq)函数把一个序列类型转换成一个列表
运算符: 索引运算[i] ,切片运算[i:j], 扩展切片运算[i:j:stride]
支持运算:索引,切片,min(),max(),len()等
1. 列表赋值:
l1=[] 空列表 #但是内存有位置存放,使用id(l1)查看,输出45623200
l2=[1,2,3,4,5,6,]
l3=[1,'b']
l4=[[1,2],['a','b']] 嵌套列表
2.列表操作
1 2 3 | l2 = [ 1 , 2 , 3 , 4 , 5 , 6 ,] l2[ 3 ] = 78 print (l2) |
输出[1, 2, 3, 78, 5, 6]
1 2 3 | l2 = [ 1 , 2 , 3 , 4 , 5 , 6 ,] l2[ 4 ] = 'xyz' print (l2) |
输出[1, 2, 3, 4, 'xyz', 6]
1 2 3 | l2 = [ 1 , 2 , 3 , 4 , 5 , 6 ,] result = l2[ 1 : 3 ] #表示分片偏移,第一个数值表示提取的第一个元素编号,包含在分片内;第二个数值表示分片后剩余的的第一个元素编号,不包含在分片内 print (result) |
输出[2, 3]
1 2 3 | l2 = [ 1 , 2 , 3 , 4 , 5 , 6 ,] l2[ 1 : 3 ] = [] #表示删除, 也可以使用del([l2[1:3]]) print (l2) |
输出[1, 4, 5, 6]
二、列表的方法

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<value. """
pass
def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value.n """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __reversed__(self): # real signature unknown; restored from __doc__
""" L.__reversed__() -- return a reverse iterator over the list """
pass
def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass
def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass
def __sizeof__(self): # real signature unknown; restored from __doc__
""" L.__sizeof__() -- size of L in memory, in bytes """
pass
__hash__ = None
1.append(self, p_object),在列表尾部追加单个对象x,使用多个参数会引起异常
1 2 3 | list = [ 1 , 2 , 3 , 4 , 5 ,] list .append( 88 ) print ( list ) |
输出[1, 2, 3, 4, 5, 88]
1 2 3 4 | l1 = [ 1 , 2 , 3 , 4 , 5 ,] l2 = [ 'carlos' , 77 ,] l1.append(l2) print (l1) |
输出[1, 2, 3, 4, 5, ['carlos', 77]]
2. clear(self)
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 ,] l1.clear() print (l1) |
输出[]
3. copy(self)
1 2 3 4 | l1 = [ 1 , 2 , 3 , 4 , 5 ,] l2 = l1.copy() print (l1) print (l2) |
输出
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
4. count(self, value),返回对象value在列表中出现的次数
1 2 3 | l1 = [ 'to' , 2 , 'be' , 'to' , 5 ,] result = l1.count( 'to' ) print (result) |
输出2
1 2 3 | l1 = [[ 1 , 2 ], 'alex' , 'be' , 'to' ,[ 1 , 2 ],[ 2 ],[ 1 , 2 ],] result = l1.count([ 1 , 2 ]) print (result) |
输出3
5. extend(self, iterable),将列表中的表项添加到列表中,返回None
1 2 3 4 5 6 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 ,] print ( id (l1)) l2 = [ 'carlos' , 99 ,[ 3 , 7 ],] l3 = l1.extend(l2,) print (l1) #extend修改被扩展序列 print ( id (l1)) |
输出
33039344
[1, 2, 3, 4, 5, 6, 'carlos', 99, [3, 7]]
33039344
1 2 3 4 5 6 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 ,] print ( id (l1)) l2 = [ 'carlos' , 99 ,[ 3 , 7 ],] l3 = l1 + l2 print (l3) # 原始连接的操作是返回一个全新的列表 print ( id (l3)) |
输出
38806512
[1, 2, 3, 4, 5, 6, 'carlos', 99, [3, 7]]
38807592
6. index(self, value, start=None, stop=None),返回列表中匹配对象value的第一个列表项的索引,无匹配元素时产生异常
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 ,] l2 = l1.index( 5 ) print (l2) # 返回列表中匹配对象value的第一个列表项的索引 |
输出4
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 ,] l2 = l1.index( 5 , 5 , 10 ) print (l2) # 返回列表中匹配对象value的索引5,10 之前匹配的项 |
输出9
7.insert(self, index, p_object),在索引为i的元素前插入对象x,如list.insert(0,x)在第一项前插入对象,返回None
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 ,] l1.insert( 3 , 'alex' ) print (l1) |
输出[1, 2, 3, 'alex', 4, 5, 6, 'carlos', 99, [3, 7], 5]
8.pop(self, index=None),删除列表中索引为x的表项,并返回该表项的值,若未指定索引,pop返回列表最后一项
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 ,] l1.pop() # 默认最后一个元素 print (l1) |
输出[1,2,3,4,5,6,'carlos',99,[3,7]]
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 ,] l1.pop( 5 ) # 移除索引5的元素 print (l1) |
输出[1,2,3,4,5,'carlos',99,[3,7],5]
9.remove(self, value),删除列表中匹配对象x的第一个元素,匹配元素时产生异常,返回None
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 , 88 , 5 , 102 ,] l1.remove( 5 ) # 移除匹配项5的一个元素 print (l1) |
输出[1,2,3,4,6,'carlos',99,[3,7],5,88,5,102,]
10. reverse(self),颠倒列表元素的顺序
1 2 3 | l1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 'carlos' , 99 ,[ 3 , 7 ], 5 , 88 , 5 , 102 ,] l1.reverse() print (l1) |
输出[102, 5, 88, 5, [3, 7], 99, 'carlos', 6, 5, 4, 3, 2, 1]
11 .sort(self, key=None, reverse=False),对列表排序,返回none,bisect模块可用于排序列表项的添加和删除
1 2 3 | l1 = [ 1 , 25 , 63 , 4 , 5 , 6 , 99 , 5 , 88 , 15 , 102 ,] l1.sort() print (l1) |
输出[1, 4, 5, 5, 6, 15, 25, 63, 88, 99, 102]
当需要一个排好序的列表副本,同时又要保留原有列表不变的正确做法如下:
1 2 3 4 5 | l1 = [ 1 , 25 , 63 , 4 , 5 , 6 , 99 , 5 , 88 , 15 , 102 ,] l2 = l1[:] l2.sort() print (l1) print (l2) |
输出
[1, 25, 63, 4, 5, 6, 99, 5, 88, 15, 102]
[1, 4, 5, 5, 6, 15, 25, 63, 88, 99, 102]
作者:CARLOS_CHIANG
出处:http://www.cnblogs.com/yaoyaojcy/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
posted on 2017-08-22 08:05 CARLOS_KONG 阅读(4117) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义