1. 程式人生 > >Python序列結構--元組

Python序列結構--元組

def append python 字符串 std rop 直接 列表 file

元組:輕量級列表

元組創建於元素訪問

>>> x = (1, 2, 3)
>>> type(x)
<class ‘tuple‘>
>>> x[0]
1
>>> x[-1]
3
>>> x[1]
2
>>> x[1] = 4 # 元組是不可變的
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘tuple‘ object does not support item assignment
>>> x = (3)
>>> x
3
>>> x[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘int‘ object is not subscriptable
>>> x = (3,) # 如果元組中只有一個元素,必須在後面多寫個逗號
>>> x[0]
3
>>> x = ()
>>> x = tuple()
>>> tuple(range(5))
(0, 1, 2, 3, 4)
>>> list(enumerate(range(5)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
>>> list(zip(range(3),‘abcdefg‘))
[(0, ‘a‘), (1, ‘b‘), (2, ‘c‘)]

元組於列表的異同點

  • 相同點

    列表和元組都屬於有序序列,都支持使用雙向索引訪問其中的元素

  • 不同的點

    元組屬於不可變序列,不可直接修改元組中的元素的值,也無法為元組增加和刪除元素,從一定程度上講,可以認為元組是輕量級列表,或者“常量列表”

>>> x = ([1,2],3)
>>> x[0][0] = 5 # 修改元組中列表元素
>>>
>>> x
([5, 2], 3)
>>> x[0].append(8) # 為元組中列表增加元素
>>> x
([5, 2, 8], 3)
>>> x[0] = x[0] + [10] # 試圖修改元組的值,失敗
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘tuple‘ object does not support item assignment
>>> x
([5, 2, 8], 3)
>>> x[0] += [10] # 拋出異常,但元組中的元素已被修改
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘tuple‘ object does not support item assignment
>>> x
([5, 2, 8, 10], 3)
>>> y = x[0]
>>> y
[5, 2, 8, 10]
>>> y += [11]
>>> y
[5, 2, 8, 10, 11]
>>> x
([5, 2, 8, 10, 11], 3)
>>> y = y + [12]
>>> y
[5, 2, 8, 10, 11, 12]
>>> x
([5, 2, 8, 10, 11], 3)
# x = x + [3] 和 x += [3]有本質區別
>>> x = [1,2]
>>> id(x)
2666094589960
>>> x += [3]>>> id(x)2666094589960>>> x[1, 2, 3]>>> x = x + [4]>>> x[1, 2, 3, 4]>>> id(x)2666094590600
  • 作為不可變序列,與整數、字符串一樣,元組可用字典的鍵,也可以作為集合的元素,內置函數hash()可以用來測試一個對象是否可哈希,如果對象不可哈希會拋出異常

    >>> hash((1,))      # 元組、數字、字符串都是可哈希的
    3430019387558
    >>> hash(3)
    3
    >>> hash(‘hello world.‘)
    -4516426368981041408
    >>> hash([1,2]) # 列表不可哈希的
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unhashable type: ‘list‘

生成器推導式

  • 生成器推導式也稱為生成器表達式,在形式上生成器推導式使用圓括號作為定界符,生成器推導式的結果是一個生成器對象,可以將其轉換為列表或者元組,也可以使用生成器對象的next()方法或者內置函數next()進行遍歷,或者使用for循環來遍歷其中的元素,只能從前往後正向訪問其中的元素,沒有任何方法可以再次訪問已訪問過的元素

    >>> g = ((i+2)**2 for i in range(10))   # 創建生成器對象
    >>> g
    <generator object <genexpr> at 0x0000026CBF9E93B8>
    >>> list(g) # 將生成器對象轉換為列表
    [4, 9, 16, 25, 36, 49, 64, 81, 100, 121]
    >>> next(g)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration
    >>> g.__next__()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration
    >>> g = ((i+2)**2 for i in range(10))
    >>> g.__next__()
    4
    >>> g.__next__()
    9
    >>> next(g)
    16
    >>> next(g)
    25
    >>> g = ((i+2)**2 for i in range(10))
    >>> tuple(g)
    (4, 9, 16, 25, 36, 49, 64, 81, 100, 121)
    >>> g = ((i+2)**2 for i in range(10))
    >>> for item in g:
    ... print(item,end=‘ ‘)
    ...
    4 9 16 25 36 49 64 81 100 121 >>>
    >>> x = filter(None,range(10))>>> 1 in xTrue>>> 5 in xTrue>>> 2 in x # 不可再訪問已經訪問過的元素False>>> x = map(str,range(10))>>> ‘0‘ in xTrue>>> ‘0‘ in xFalse

Python序列結構--元組