網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1. enumerate()介紹
enumerate()是python的內(nèi)置函數(shù),適用于python2.x和python3.x;
enumerate在字典賞識(shí)枚舉、列舉的意思;
enumerate參數(shù)為可遍歷/可迭代的對(duì)象(如列表、字符串);
enumerate多用于在for循環(huán)中得到計(jì)數(shù),利用它可以同時(shí)獲得索引和值,即需要 index 和 value 值的時(shí)候可以使用enumerate;
enumerate()返回的是一個(gè)enumerate對(duì)象 。如:
s = [1, 2, 3, 4, 5]
e = enumerate(s)
print(e)
# <enumerate object at 0x0000028246E94C28>
2. enumerate的使用:
例如:已知s = [1,2,3,4,5,6],要求輸出: 0,1 1,2 2,3 3,4 4,5 5,6
例1:
s = [1, 2, 3, 4, 5]
x = enumerate(s)
for index,value in x:
print("%s,%s"%(index,value))
# 0,1
# 1,2
# 2,3
# 3,4
# 4,5
例2:
s = [1, 2, 3, 4, 5]
# 索引從1開始
for index,value in enumerate(s, 1):
print("%s,%s" %(index,value))
# 1,1
# 2,2
# 3,3
# 4,4
# 5,5
3. 例子
將一組字典列表中的某一對(duì)字典元素與另一組字典列表中某一對(duì)字典元素組成一組新的字典列表
notes = [{'id': '11111111111', 'content': 'lalallalalalallala'}, {'id': '2222222222222', 'content': 'qqqqqqqqqqqqqqqqqqq'}, {'id': '33333333333333333', 'content': 'aaaaaaaaaaaaaaaaa'}]
ids = [one_note["id"] for one_note in notes]
contents = [one_note["content"] for one_note in notes]
result = [{'id': '11111111111', 'label': 'positive'}, {'id': '2222222222222', 'label': 'positive'}, {'id': '33333333333333333', 'label': 'negative'}]
finally_result = [{"id": ids[index], "label": label["label"]} for index,label in enumerate(result)]
print(finally_result)
# [{'id': '11111111111', 'label': 'positive'}, {'id': '2222222222222', 'label': 'positive'}, {'id': '33333333333333333', 'label': 'negative'}]
詳情解析:
notes = [{'id': '11111111111', 'content': 'lalallalalalallala'}, {'id': '2222222222222', 'content': 'qqqqqqqqqqqqqqqqqqq'}, {'id': '33333333333333333', 'content': 'aaaaaaaaaaaaaaaaa'}]
ids = [one_note["id"] for one_note in notes]
contents = [one_note["content"] for one_note in notes]
print(type(ids)) # <class 'list'>
print(ids)
# ['11111111111', '2222222222222', '33333333333333333']
print(type(contents)) # <class 'list'>
print(contents)
# ['lalallalalalallala', 'qqqqqqqqqqqqqqqqqqq', 'aaaaaaaaaaaaaaaaa']
result = [{'id': '11111111111', 'label': 'positive'}, {'id': '2222222222222', 'label': 'positive'}, {'id': '33333333333333333', 'label': 'negative'}]
finally_result = [{"id": ids[index], "label": label["label"]} for index,label in enumerate(result)]
print(type(finally_result)) # <class 'list'>
print(finally_result)
# [{'id': '11111111111', 'label': 'positive'}, {'id': '2222222222222', 'label': 'positive'}, {'id': '33333333333333333', 'label': 'negative'}]
原文鏈接:https://blog.csdn.net/weixin_39589455/article/details/130505471
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-03-28 深入理解numpy中argmax的具體使用_python
- 2022-04-18 使用numpy對(duì)數(shù)組求平均時(shí)如何忽略nan值_python
- 2022-07-27 Python編寫條件分支代碼方法_python
- 2022-03-18 linux下修改文件權(quán)限chmod命令詳細(xì)解析_Linux
- 2022-05-13 Django-跨域問題Csrf
- 2022-05-16 C++Smart?Pointer?智能指針詳解_C 語(yǔ)言
- 2022-04-09 idea解決sun.misc.BASE64Encoder 類找不到報(bào)錯(cuò)
- 2022-08-02 使用shell讀取ini文件方法步驟_linux shell
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支