• 定义:Python列表对象是这个语言提供的最通用的序列。列表是一个任意类型的对象的位置相关的有序集合,它没有固定大小。不像字符串,其大小是可变的,通过对偏移量进行赋值以及其他各种列表的方法调用,确实能够修改列表的大小。
  • 序列操作。由于列表是序列的一种,列表支持我们对字符串所讨论过的序列操作。唯一的区别就是结果往往是列表而非字符串。
    #ipython3
    
    In [1]: L = [123,'spam',1.23]
    
    In [2]: len(L)
    Out[2]: 3
    
    In [2]: len(L)
    Out[2]: 3
    
    In [3]: L[0]
    Out[3]: 123
    
    In [4]: L[:-1]
    Out[4]: [123, 'spam']
    
    In [5]: L + [4,5,6]
    Out[5]: [123, 'spam', 1.23, 4, 5, 6]
    
    In [6]: L
    Out[6]: [123, 'spam', 1.23]
    
  • 类型特定操作。Python的列表与其他语言的数组有些相似,但是列表要强大的多。其中一个方面就是,列表没有固定类型的约束。没有固定大小。
    In [7]: L.append('NI')
    
    In [8]: L
    Out[8]: [123, 'spam', 1.23, 'NI']
    
    In [9]: L.pop(2)
    Out[9]: 1.23
    
    In [10]: L
    Out[10]: [123, 'spam', 'NI']
    

    sort方法实现升序对列表排序,reverse对列表实现翻转。

    	
    In [1]: M = ['cc','aa','bb']
    
    In [2]: M.sort()
    
    In [3]: M
    Out[3]: ['aa', 'bb', 'cc']
    
    In [4]: M.reverse()
    
    In [5]: M
    Out[5]: ['cc', 'bb', 'aa']
    
  • 边界检查。尽管列表没有固定大小,Python仍不允许引用不存在的元素。超过列表末尾之外的索引会报错,对列表末尾范围之外的赋值也是如此。
    In [11]: L
    Out[11]: [123, 'spam', 'NI']
    
    In [12]: L[99]
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
     in ()
    ----> 1 L[99]
    
    IndexError: list index out of range
    
    In [13]: L[99] = 1
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
     in ()
    ----> 1 L[99] = 1
    
    IndexError: list assignment index out of range
    
  • 嵌套。Python核心数据类型的一个优秀特性就是他们支持任意的嵌套。能够以任意的组合对其进行嵌套,并可以多个层次进行嵌套。如让一个列表包含一个字典,并在这个字典中包含另一个列表等,这种特性的一个直接应用就是实现矩阵,或者Python中的多维数组。
    In [1]: M = [[1,2,3],
       ...: [4,5,6],
       ...: [7,8,9]]
    
    In [2]: M
    Out[2]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    In [3]: M[1]
    Out[3]: [4, 5, 6]
    
    In [4]: M[1][2]
    Out[4]: 6
    
  • 列表解析表达式。例如我们需要从列举的矩阵中提取第二列,因为矩阵式按照行进行存储的,所以通过简单的索引即可获取行,使用列表解析可以同样简单地获得列。
    In [5]: col2 = [row[1] for row in M]
    
    In [6]: col2
    Out[6]: [2, 5, 8]
    
    In [7]: M
    Out[7]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    

    列表解析源自集合的概念,它是通过对序列中的每一项运行一个表达式来创建一个新的列表的方法,每次一个,从左至右。列表解析式编写在方括号中的,并且由使用了同一个变量名的(这里指row)表达式和循环结构构成。实际应用可能更复杂

    In [8]: [row[1] + 1 for row in M]
    Out[8]: [3, 6, 9]
    
    In [9]: [row[1] for row in M if row[1] % 2 == 0]
    Out[9]: [2, 8]
    

    列表解析创建了新的列表作为结果,但是能够在任何可迭代的对象上进行迭代。例如,我们使用一个列表解析去步进坐标的一个硬编码列表和一个字符串

    In [10]: diag = [M[i][i] for i in [0,1,2]]
    
    In [11]: diag
    Out[11]: [1, 5, 9]
    
    In [12]: doubles = [c * 2 for c in 'spam']
    
    In [13]: doubles
    Out[13]: ['ss', 'pp', 'aa', 'mm']
    

    列表解析以及相关的内容函数map和filter比较复杂。列表解析是一个可选的特性,在实际应用中比较方便,并常常具有处理速度上的优势,他们也能够在Python的任何的序列类型中发挥作用,甚至一些不属于序列的类型。

    在新版Python中,括号中的解析语法也可以用来创建产生所需结果的生成器,例如,内置的sum函数,按一种顺序汇总各项

    In [14]: G = (sum(row) for row in M)
    
    In [15]: next(G)
    Out[15]: 6
    
    In [16]: next(G)
    Out[16]: 15
    
    In [17]: next(G)
    Out[17]: 24
    
    In [18]: next(G)
    ---------------------------------------------------------------------------
    StopIteration                             Traceback (most recent call last)
     in ()
    ----> 1 next(G)
    
    StopIteration: 
    

    内置函数map可以做类似事情,产生对各项运行一个函数的结果,在Python3中,将其包装到列表中,会使其返回所有值:

    In [19]: list(map(sum,M))
    Out[19]: [6, 15, 24]
    

    在Python3中,解析语法也可以用来创建集合和字典:

    In [20]: {sum(row) for row in M}
    Out[20]: {6, 15, 24}
    
    In [21]: {i : sum(M[i]) for i in range(3)} 
    Out[21]: {0: 6, 1: 15, 2: 24}
    

    实际上,在Python3中,列表,集合和字典都可以用解析来创建

    #ipython3
    
    In [1]: [ord(x) for x in 'spaam']
    Out[1]: [115, 112, 97, 97, 109]
    
    In [2]: {ord(x) for x in 'spaam'}
    Out[2]: {97, 109, 112, 115}
    
    In [3]: {x:ord(x) for x in 'spaam'}
    Out[3]: {'a': 97, 'm': 109, 'p': 112, 's': 115}
    
分类: Python3

6 条评论

177slots · 2025-12-19 20:13

Yo, 177slots, you always have the latest stuff! Always a fun time checking out the latest games that come out. Check it out for yourself here: 177slots

rivalry · 2025-12-24 10:55

Checked out rivalry.info. Seems to be more about the general idea of rivalry rather than a specific betting platform. So you know you can visit this site to get information on rivalry. Here is the link: rivalry

jilibet004 · 2026-01-14 16:15

jilibet004 https://www.jilibet004.org

pesomaxfun · 2026-01-14 17:18

pesomaxfun https://www.elpesomaxfun.com

playpal77 · 2026-01-14 21:19

playpal77 https://www.playpal77sy.org

beeph · 2026-02-10 06:31

[7527]Beeph Official Casino: Secure Beeph Login, Register & Top Beeph Slot Online. Get the Beeph App Download Today! Join Beeph Official Casino for the ultimate gaming experience in the Philippines. Secure Beeph login, easy Beeph register, and premium Beeph slot online titles await. Get the Beeph app download today to win big! visit: beeph

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注