Python3中zip()函數(shù)知識點(diǎn)小結(jié)
1.引言
在本文中,我將帶領(lǐng)大家深入了解Python中的zip()函數(shù),使用它可以提升大家的工作效率。
閑話少說,我們直接開始吧!
2. 基礎(chǔ)知識
首先,我們來介紹一些基礎(chǔ)知識點(diǎn):
Python中的某些數(shù)據(jù)類型是不可變的(例如字符串、整數(shù)),而有些數(shù)據(jù)類型是可變的(如列表和字典)。不可變的數(shù)據(jù)對象在創(chuàng)建后不能更改,可變對象可以更改。
可迭代對象是一個單獨(dú)返回其每個成員元素的對象。比如列表、元組、字符串和字典都是可迭代的對象。我們可以使用iter()或for循環(huán)來迭代可迭代對象。
當(dāng)一個對象返回迭代器時,我們必須使用它來檢索一個我們可以看到或使用的對象。
3. 向zip函數(shù)傳遞參數(shù)
我們可以在函數(shù)zip()中傳遞任意數(shù)量的可迭代項:
3.1 傳遞零個參數(shù)
樣例如下:
>>> zipped = zip() >>> list(zipped) []
上述代碼中,我們向函數(shù)zip()傳遞了零個元素,此時該函數(shù)返回空。
3.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.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
3.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()之前,檢查可迭代項的長度非常重要。
4. 總結(jié)
本文重點(diǎn)介紹了Python中關(guān)于zip函數(shù)的基礎(chǔ)知識點(diǎn)總結(jié),并給出了相應(yīng)的代碼示例。
到此這篇關(guān)于Python3中zip()函數(shù)知識點(diǎn)小結(jié)的文章就介紹到這了,更多相關(guān)Python3中zip()函數(shù) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python機(jī)器學(xué)習(xí)入門(一)序章
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)入門知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Linux系統(tǒng)中設(shè)置Python程序開機(jī)啟動的兩種方式
在 Linux 系統(tǒng)中設(shè)置Python 腳本開機(jī)啟動,通常可以通過以下幾種方式實現(xiàn), 使用 systemd(推薦方式)和使用 crontab(對于簡單任務(wù)),文章通過代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2024-05-05Python continue繼續(xù)循環(huán)用法總結(jié)
本篇文章給大家總結(jié)了關(guān)于Python continue繼續(xù)循環(huán)的相關(guān)知識點(diǎn)以及用法,有需要的朋友跟著學(xué)習(xí)下吧。2018-06-06Python中使用pprint函數(shù)進(jìn)行格式化輸出的教程
這篇文章主要介紹了Python中使用pprint函數(shù)進(jìn)行格式化輸出的教程,包括能夠控制輸出寬度等非常有用的特性,需要的朋友可以參考下2015-04-04python使用selenium操作瀏覽器的實現(xiàn)示例
Selenium是一個模擬瀏覽器瀏覽網(wǎng)頁的工具,主要用于測試網(wǎng)站的自動化測試工具,本文主要介紹了python使用selenium操作瀏覽器的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-01-01解決pycharm無法識別本地site-packages的問題
今天小編就為大家分享一篇解決pycharm無法識別本地site-packages的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10