从词典中的列表创建词典 [英] Creating Dictionaries from Lists inside of Dictionaries

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

问题描述

我对Python还是很陌生,我被一个看似简单的任务所困扰. 在我的程序的一部分中,我想从列表内部的值创建二级字典",这些值是一级字典"的值.

I'm quite new to Python and I have been stumped by a seemingly simple task. In part of my program, I would like to create Secondary Dictionaries from the values inside of lists, of which they are values of a Primary Dictionary.

我还想将这些值默认设置为0

I would also like to default those values to 0

为简单起见,主要字典看起来像这样:

For the sake of simplicity, the Primary Dictionary looks something like this:

primaryDict = {'list_a':['apple', 'orange'], 'list_b':['car', 'bus']}

我希望得到的结果是这样的:

What I would like my result to be is something like:

{'list_a':[{'apple':0}, {'orange':0}], 'list_b':[{'car':0}, {'bus':0}]}

我知道该过程应该是遍历primaryDict中的每个列表,然后遍历该列表中的项目,然后将它们分配为Dictionary.

I understand the process should be to iterate through each list in the primaryDict, then iterate through the items in the list and then assign them as Dictionaries.

我尝试了许多"for"循环的变体,它们看起来都类似于:

I've tried many variations of "for" loops all looking similar to:

for listKey in primaryDict:
    for word in listKey:
        {word:0 for word in listKey}

我还尝试了一些将Dictionary和List理解相结合的方法, 但是当我尝试索引和打印字典时,例如:

I've also tried some methods of combining Dictionary and List comprehension, but when I try to index and print the Dictionaries with, for example:

print(primaryDict['list_a']['apple'])

我得到"TypeError:列表索引必须是整数或切片,而不是str",我将其解释为"apple"实际上不是字典,而是列表中的字符串.我通过将'apple'替换为0进行测试,它只返回'apple',证明它是正确的.

I get the "TypeError: list indices must be integers or slices, not str", which I interpret that my 'apple' is not actually a Dictionary, but still a string in a list. I tested that by replacing 'apple' with 0 and it just returns 'apple', proving it true.

在以下方面,我需要帮助:

I would like help with regards to:

-是否将列表中的值分配为"0"值的字典

-Whether or not the values in my list are assigned as Dictionaries with value '0'

-无论是我的索引错误(在循环还是在打印功能中),以及我的误解

-Whether the mistake is in my indexing (in the loop or the print function), and what I am mistaken with

-我所做的一切都无法获得理想的结果,我应该尝试一种不同的方法

-Everything I've done won't get me the desired outcome and I should attempt a different approach

谢谢

推荐答案

您可以通过以下方式获取所需的数据结构:

You can get the data structure that you desire via:

primaryDict = {'list_a':['apple', 'orange'], 'list_b':['car', 'bus']}
for k, v in primaryDict.items():
    primaryDict[k] = [{e: 0} for e in v]

# primaryDict
{'list_b': [{'car': 0}, {'bus': 0}], 'list_a': [{'apple': 0}, {'orange': 0}]}    

但是正确的嵌套访问将是:

But the correct nested access would be:

print(primaryDict['list_a'][0]['apple'])  # note the 0

如果您确实希望primaryDict['list_a']['apple']工作,请改为

If you actually want primaryDict['list_a']['apple'] to work, do instead

for k, v in primaryDict.items():
    primaryDict[k] = {e: 0 for e in v}

# primaryDict
{'list_b': {'car': 0, 'bus': 0}, 'list_a': {'orange': 0, 'apple': 0}}

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

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