最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3中zip()函數(shù)知識(shí)點(diǎn)小結(jié)

 更新時(shí)間:2023年02月28日 08:30:20   作者:趙卓不凡  
本文主要介紹了Python3中zip()函數(shù)知識(shí)點(diǎn)小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.引言

在本文中,我將帶領(lǐng)大家深入了解Python中的zip()函數(shù),使用它可以提升大家的工作效率。
閑話少說,我們直接開始吧!

2. 基礎(chǔ)知識(shí)

首先,我們來(lái)介紹一些基礎(chǔ)知識(shí)點(diǎn):

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

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

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

3.1 傳遞零個(gè)參數(shù)

樣例如下:

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

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

3.2 傳遞一個(gè)參數(shù)

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

示例代碼如下:

# 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)建了一個(gè)列表,其中有三個(gè)字符串表示三個(gè)學(xué)生的姓名。

3.3 傳遞兩個(gè)參數(shù)

傳遞兩個(gè)參數(shù)將創(chuàng)建一個(gè)具有成對(duì)的元組集合,其中第一個(gè)元素來(lái)自第一個(gè)參數(shù),第二個(gè)元素來(lái)自第二個(gè)參數(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)建了另一個(gè)包含三個(gè)字符串的列表。此時(shí),每個(gè)元素用于表示每個(gè)學(xué)生student_names的對(duì)應(yīng)student_ids。

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

>>> 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 傳遞長(zhǎng)度不等的參數(shù)

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

# 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對(duì)student_ids中的最后一個(gè)元素1沒有做任何操作。因此,在傳遞給zip()之前,檢查可迭代項(xiàng)的長(zhǎng)度非常重要。

4. 總結(jié)

本文重點(diǎn)介紹了Python中關(guān)于zip函數(shù)的基礎(chǔ)知識(shí)點(diǎn)總結(jié),并給出了相應(yīng)的代碼示例。

到此這篇關(guān)于Python3中zip()函數(shù)知識(shí)點(diǎn)小結(jié)的文章就介紹到這了,更多相關(guān)Python3中zip()函數(shù) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

南陵县| 岑巩县| 河南省| 达州市| 新安县| 安多县| 蒙阴县| 麦盖提县| 闵行区| 南郑县| 天峨县| 峨边| 安阳市| 独山县| 泽州县| 黑山县| 奉新县| 红原县| 屯门区| 久治县| 新营市| 镇巴县| 中方县| 肥东县| 屏东县| 仪陇县| 光泽县| 刚察县| 淮阳县| 廊坊市| 南丹县| 库伦旗| 乐业县| 历史| 阜南县| 万荣县| 越西县| 澳门| 古交市| 常宁市| 申扎县|