【问题标题】:Python dictionary with list elements as keys and the elements of another list as the values? [duplicate]Python字典以列表元素作为键,另一个列表的元素作为值? [复制]
【发布时间】:2013-02-16 15:04:15
【问题描述】:

所以我想制作一个 Python 字典,其中一个列表的元素作为键,另一个列表的列表元素作为值,这可能吗?

这样:

list1 = ('red', 'blue', 'green', many more strings )
list2 = (1, 2, 3, many more values)

d = { list1[0:]: list2[0:] }

这显然行不通,但有类似的东西吗?

【问题讨论】:

    标签: python list dictionary


    【解决方案1】:

    你可以这样做:

    >>> d = dict(zip(list1, list2))
    {'blue': 2, 'green': 3, 'red': 1}
    

    【讨论】:

    • 好吧,抱歉,我真的很想找到答案,但我想我一定是错过了!太有趣了,你可以减去声望点,搞笑大声笑谢谢你的回答!我会试试这个。
    猜你喜欢
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多