網站首頁 編程語言 正文
1. enumerate()介紹
enumerate()是python的內置函數,適用于python2.x和python3.x;
enumerate在字典賞識枚舉、列舉的意思;
enumerate參數為可遍歷/可迭代的對象(如列表、字符串);
enumerate多用于在for循環中得到計數,利用它可以同時獲得索引和值,即需要 index 和 value 值的時候可以使用enumerate;
enumerate()返回的是一個enumerate對象 。如:
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. 例子
將一組字典列表中的某一對字典元素與另一組字典列表中某一對字典元素組成一組新的字典列表
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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-20 Postgresql刪除數據庫表中重復數據的幾種方法詳解_PostgreSQL
- 2022-05-31 postman批量執行接口測試的圖文步驟_其它綜合
- 2024-03-19 maven本地倉庫有包,導致could not find artifact
- 2022-04-03 帶你理解C語言中的漢諾塔公式_C 語言
- 2022-08-02 Redis實現登錄注冊的示例代碼_Redis
- 2022-11-14 python中numpy?常用操作總結_python
- 2022-10-27 Apache?Hive?通用調優featch抓取機制?mr本地模式_Linux
- 2022-10-22 Android協程作用域與序列發生器限制介紹梳理_Android
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支