将两个列表合并到python中的字典中 [英] Combining two lists into a dictionary in python

查看:110
本文介绍了将两个列表合并到python中的字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有两个列表:

  listA = [1,2,3,4,5] 
listB = [红,蓝,橙,黑,灰]

找出如何在两列中显示两个参数列表中的元素,
分配 1:red,2:blue ... 等等。 p>

这不需要使用内置的 zip 函数就可以完成。

解决方案

 >>> listA = [1,2,3,4,5] 
>>> listB = [red,blue,orange,black,gray]
>>> dict(zip(listA,listB))
{1:'red',2:'blue',3:'orange',4:'black',5:'gray'}


For Example, if I had two lists:

listA = [1, 2, 3, 4, 5]
listB = [red, blue, orange, black, grey]

I'm trying to figure out how to display the elements in the two argument lists in two columns, assigning 1: red, 2: blue... and so on.

This has to be done without using the built-in zipfunction.

解决方案

 >>> listA = [1, 2, 3, 4, 5]
 >>> listB = ["red", "blue", "orange", "black", "grey"]
 >>> dict(zip(listA, listB))
 {1: 'red', 2: 'blue', 3: 'orange', 4: 'black', 5: 'grey'}

这篇关于将两个列表合并到python中的字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆