织梦CMS - 轻松建站从此开始!

欧博ABG-会员注册-官网网址

欧博官网Zipping a Python Dictionary and List together

时间:2026-01-12 19:07来源: 作者:admin 点击: 1 次
Note: do not shadow the built-in dict. This will one day come back to haunt you. Now, as for your issue, simply use dict.items: >>> d = {'A

Note: do not shadow the built-in dict. This will one day come back to haunt you.

Now, as for your issue, simply use dict.items:

>>> d = {'A':1, 'B':2, 'C':'3'} >>> num_list = [1, 2,3 ] >>> for (key, value), num in zip(d.items(), num_list): ... print(key) ... print(value) ... print(num) ... A 1 1 C 3 2 B 2 3 >>>

Note: Dictionaries aren't ordered, so you have no guarantee on the order of the items when iterating over them.

Additional Note: when you iterate over a dictionary, it iterates over the keys:

>>> for k in d: ... print(k) ... A C B

Which makes this common construct:

>>> for k in d.keys(): ... print(k) ... A C B >>>

Redundant, and in Python 2, inefficient.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2026-01-13 01:01 最后登录:2026-01-13 01:01
栏目列表
推荐内容