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

欧博ABG官网-欧博官方网址-会员登入

How do I combine two lists into aDG游戏 dictionary i

时间:2026-01-12 19:09来源: 作者:admin 点击: 3 次
If there are duplicate keys in the first list that map to different values in the second list, like a 1-to-many relationship, but you need the values

If there are duplicate keys in the first list that map to different values in the second list, like a 1-to-many relationship, but you need the values to be combined or added or something instead of updating, you can do this:

i = iter(["a", "a", "b", "c", "b"]) j = iter([1,2,3,4,5]) k = list(zip(i, j)) for (x,y) in k: if x in d: d[x] = d[x] + y #or whatever your function needs to be to combine them else: d[x] = y

In that example, d == {'a': 3, 'c': 4, 'b': 8}

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