Python-列表字典 [英] Python - dictionary of lists

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

问题描述

制作列表字典的最佳方法是哪一种? 例如,如果我有列表 list1,list2 并想要制作这样的字典 my_dict :

Which is the best way to make a dictionary of lists? For instance, if I have lists list1, list2 and want to make a dictionary my_dict like that:

my_dict = ['list1': list1, 'list2': list2]

我找到了示例,但是最好的答案是这样写的2009年.也许有一些新的简单方法可以做到这一点?

I've found this example but the best answer is written in 2009. Maybe there are some new more laconic ways to do this?

推荐答案

您需要使用大括号而不是方括号,但否则可能会和它一样好:

You need to use curly rather than square brackets, but otherwise this is probably as good as it gets:

list1 = ['a', 'b', 'c']
list2 = [1, 2, 3, 4]
my_dict = {'list1': list1, 'list2': list2}

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

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