创建和索引2D词典 [英] Creating and indexing a 2D dictionary

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

问题描述

这只是我的一个简短摘要,因为我的代码太复杂而无法进入,但是我基本上想要的是:

This is just a quick snippet I made since my code is too complicated to get into, but what I basically want is:

  1. 以名称为键且值为空列表的字典
  2. 在该空白列表中附加问题列表
  3. 这些问题是关键,它们的值是一个空列表
  4. 将一个答案列表添加到该空列表

我能够做到这一点,但是我的问题是要引用他们询问密钥或为其分配任何内容,除了密钥之外,我还必须引用该列表中的索引.

I was able to do this, but my issue is that to reference they question key or assign anything to it, I have to reference its index in that list in addition to the key.

它们必须在列表中,因为每个名称都将包含一个问题列表,并且每个问题都具有一个答案列表,例如:

They need to be in lists because each name will have a list of questions, and each question has a list of answers, like so: image

这里的代码显示了我如何编写字典并将其添加到它,在实际代码中这是循环执行的,但这是它的要旨: 图像2

This code here shows how I'm making my dict and appending to it, in the actual code this is done in loops but this is the jist of it: image2

是否有一种方法可以避免必须按数字对问题列表进行索引? (因此我可以做上面的事情而不会出错)

Is there a way to get around having to index the list of questions by number? (so I can do what I did above without getting an error)

更新:

我开始意识到这是一种祝福,因为数字索引是检查问题及其答案的快速方法.如果索引为[0],它将显示单个问题及其所有答案.这是有帮助的,因为问题是句子,键入内容可能会很麻烦.

I'm starting to realize that this is a blessing since the number index is a quick way to check the question and it's answers. If I index [0] it'll show the the single question and all its answers. Which is helpful since the questions are sentences and typing them could be a pain.

我仍然想知道我的问题是否有答案,因此无论如何我都会发布.

I'm still curious if there's an answer to my question though, so I'll post this anyways.

文本代码:

>names = ["john","mark","jake"]
>namesDict = {x:[] for x in names}
>namesDict
{'mark': [], 'jake': [], 'john': []}
>namesDict["mark"].append({"question":[]})
>namesDict
{'mark': [{'question': []}], 'jake': [], 'john': []}
>namesDict["mark"]["question"].append("answer")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: list indices must be integers, not str
>namesDict["mark"][0]["question"].append("answer")
>namesDict
{'mark': [{'question': ['answer']}], 'jake': [], 'john': []}

推荐答案

我认为您在这里寻找的是跳过列表并嵌套一些词典.看起来像这样:

I think what you're looking for here is to skip the lists and nest up some dictionaries. It would look something like this:

names = ["john","mark","jake"]
namesDict = {x: {} for x in names}

namesDict['mark']['question1'] = []

namesDict['mark']['question1'].extend(('ans1_1', 'ans1_2', 'ans1_3'))
print(namesDict)  # {'john': {}, 'mark': {'question1': ['ans1_1', 'ans1_2', 'ans1_3']}, 'jake': {}}

namesDict['mark']['question2'] = []
namesDict['mark']['question2'].extend(('ans2_1', 'ans2_2', 'ans2_3'))
print(namesDict)  # {'john': {}, 'mark': {'question1': ['ans1_1', 'ans1_2', 'ans1_3'], 'question2': ['ans2_1', 'ans2_2', 'ans2_3']}, 'jake': {}}

唯一使您退缩的是namesDict['mark']是否具有多个与question1匹配的值.

The only thing holding you back is if namesDict['mark'] will have multiple values that match to question1.

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

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