溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python3中怎么向zip()函數(shù)傳遞參數(shù)

發(fā)布時間:2023-02-28 10:21:55 來源:億速云 閱讀:74 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Python3中怎么向zip()函數(shù)傳遞參數(shù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Python3中怎么向zip()函數(shù)傳遞參數(shù)”吧!

    基礎(chǔ)知識

    首先,我們來介紹一些基礎(chǔ)知識點:

    Python中的某些數(shù)據(jù)類型是不可變的(例如字符串、整數(shù)),而有些數(shù)據(jù)類型是可變的(如列表和字典)。不可變的數(shù)據(jù)對象在創(chuàng)建后不能更改,可變對象可以更改。
    可迭代對象是一個單獨返回其每個成員元素的對象。比如列表、元組、字符串和字典都是可迭代的對象。我們可以使用iter()或for循環(huán)來迭代可迭代對象。
    當(dāng)一個對象返回迭代器時,我們必須使用它來檢索一個我們可以看到或使用的對象。

    向zip函數(shù)傳遞參數(shù)

    我們可以在函數(shù)zip()中傳遞任意數(shù)量的可迭代項:

    1 傳遞零個參數(shù)

    樣例如下:

    >>> zipped = zip()
    >>> list(zipped)
    []

    上述代碼中,我們向函數(shù)zip()傳遞了零個元素,此時該函數(shù)返回空。

    2 傳遞一個參數(shù)

    傳遞一個參數(shù)會創(chuàng)建一個元組集合,每個元組中都有一個元素。

    示例代碼如下:

    # create a list of student names
    >>> student_names = ['Lindsay', 'Harry', 'Peter']
    # zip the list 
    >>> zipped  = zip(student_names)
    # consume with list()
    >>> list(zipped)
    [('Lindsay',), ('Harry',), ('Peter',)]

    在上述代碼中,我們創(chuàng)建了一個列表,其中有三個字符串表示三個學(xué)生的姓名。

    3 傳遞兩個參數(shù)

    傳遞兩個參數(shù)將創(chuàng)建一個具有成對的元組集合,其中第一個元素來自第一個參數(shù),第二個元素來自第二個參數(shù)。

    示例代碼如下:

    # create a list of student ids 
    >>> student_ids = ['123', '4450', '5600']
    # create a list of student names again, so that we do not forget the earlier steps!
    >>> student_names = ['Lindsay', 'Harry', 'Peter']
    # zip the lists 
    >>> zipped  = zip(student_names, student_ids)
    >>> list(zipped)
    [('Lindsay', '123'), ('Harry', '4450'), ('Peter', '5600')]

    在上述代碼中,我們創(chuàng)建了另一個包含三個字符串的列表。此時,每個元素用于表示每個學(xué)生student_names的對應(yīng)student_ids。

    此時,我們可以使用for循環(huán)來遍歷訪問,樣例代碼如下:

    >>> student_names = ['Lindsay', 'Harry', 'Peter']
    >>> student_ids = ['123', '4450', '5600']
    >>> for student_name, student_id in zip(student_names, student_ids): 
    ...     print(student_name, student_id)
    ... 
    Lindsay 123
    Harry 4450
    Peter 5600

    4 傳遞長度不等的參數(shù)

    到目前為止,我們只研究了每個可迭代項長度相同的示例:包含學(xué)生姓名和id的列表長度都是3,但我們也可以傳遞不同長度的可迭代項。此時,zip函數(shù)將返回一個元組集合,其中元組的數(shù)量等于長度最小的可迭代項。它將忽略長度較長的可迭代項中的其余元素,如下所示:

    # student_ids is a list with 4 elements 
    >>> student_ids = ['123', '4450', '5600', '1']
    # student_namdes is a list with 3 elements 
    >>> student_names = ['Lindsay', 'Harry', 'Peter']
    # zip is completely ignoring the last element of student_ids 
    >>> list(zip(student_names, student_ids))
    [('Lindsay', '123'), ('Harry', '4450'), ('Peter', '5600')]
    
    >>> for student_name, student_id in zip(student_names, student_ids): 
    ...     print(student_name, student_id)
    ... 
    Lindsay 123
    Harry 4450
    Peter 5600

    從上面的示例中可以看到,函數(shù)zip對student_ids中的最后一個元素1沒有做任何操作。因此,在傳遞給zip()之前,檢查可迭代項的長度非常重要。

    感謝各位的閱讀,以上就是“Python3中怎么向zip()函數(shù)傳遞參數(shù)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Python3中怎么向zip()函數(shù)傳遞參數(shù)這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI

    1.