網站首頁 編程語言 正文
1.引言
在本文中,我將帶領大家深入了解Python中的zip()函數,使用它可以提升大家的工作效率。
閑話少說,我們直接開始吧!
2. 基礎知識
首先,我們來介紹一些基礎知識點:
Python中的某些數據類型是不可變的(例如字符串、整數),而有些數據類型是可變的(如列表和字典)。不可變的數據對象在創建后不能更改,可變對象可以更改。
可迭代對象是一個單獨返回其每個成員元素的對象。比如列表、元組、字符串和字典都是可迭代的對象。我們可以使用iter()或for循環來迭代可迭代對象。
當一個對象返回迭代器時,我們必須使用它來檢索一個我們可以看到或使用的對象。
3. 向zip函數傳遞參數
我們可以在函數zip()中傳遞任意數量的可迭代項:
3.1 傳遞零個參數
樣例如下:
>>> zipped = zip()
>>> list(zipped)
[]
上述代碼中,我們向函數zip()傳遞了零個元素,此時該函數返回空。
3.2 傳遞一個參數
傳遞一個參數會創建一個元組集合,每個元組中都有一個元素。
示例代碼如下:
# 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',)]
在上述代碼中,我們創建了一個列表,其中有三個字符串表示三個學生的姓名。
3.3 傳遞兩個參數
傳遞兩個參數將創建一個具有成對的元組集合,其中第一個元素來自第一個參數,第二個元素來自第二個參數。
示例代碼如下:
# 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')]
在上述代碼中,我們創建了另一個包含三個字符串的列表。此時,每個元素用于表示每個學生student_names的對應student_ids。
此時,我們可以使用for循環來遍歷訪問,樣例代碼如下:
>>> 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 傳遞長度不等的參數
到目前為止,我們只研究了每個可迭代項長度相同的示例:包含學生姓名和id的列表長度都是3,但我們也可以傳遞不同長度的可迭代項。此時,zip函數將返回一個元組集合,其中元組的數量等于長度最小的可迭代項。它將忽略長度較長的可迭代項中的其余元素,如下所示:
# 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
從上面的示例中可以看到,函數zip對student_ids中的最后一個元素1沒有做任何操作。因此,在傳遞給zip()之前,檢查可迭代項的長度非常重要。
4. 總結
本文重點介紹了Python中關于zip函數的基礎知識點總結,并給出了相應的代碼示例。
原文鏈接:https://blog.csdn.net/sgzqc/article/details/129229033
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-16 python3?如何解壓縮.gz文件_python
- 2022-06-13 ASP.NET?Core?MVC路由(Routing)的用法_基礎應用
- 2022-10-02 SpringBoot前端后端分離之Nginx服務器下載安裝過程_nginx
- 2023-11-14 樹莓派以及linux ubuntu 上,各種依賴不滿足,修復不了:E: Release file f
- 2022-11-20 C#實現公式計算驗證碼的示例詳解_C#教程
- 2022-05-15 C++11:搞清楚萬能引用和右值引用
- 2023-03-18 python中統計相同字符的個數方法實例_python
- 2022-12-21 C++強制轉換與智能指針示例詳解_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支