如何根据从数据库查询的数据创建一个带有键和多个值的字典。

huangapple go评论69阅读模式
英文:

How to create a dictionary with key and multiple values based on the data queried from database

问题

以下是您要翻译的内容:

New to python and was wondering how to create a dictionary with key and multiple values so that I can pass it to HTML to display it on the UI. Below is the data format.

Questions from questions table -

[{'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id':  1, 'is_long_survey': 0},
    {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0},
    {'question_id': 3, 'question_text': 'Where is the infrastructure located?', 'category_id': 1, 'is_long_survey': 0}]"

Choices for these questions come from choice table with question_id as the foreign key.

[{'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
 {'choice_id' : 2, 'choice_text': '4', 'question_id': 1},
 {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
 {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
 {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
 {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
 {'choice_id' : 7, 'choice_text': 'local', 'question_id': 3}]

So each question has lets say 3 choices as shown below.

To display question text and their choices on the UI, how do I create a dictionary of question text as key and may be choices as multiple array values? Any other data structure I can use to format the data?

英文:

New to python and was wondering how to create a dictionary with key and multiple values so that I can pass it to HTML to display it on the UI. Below is the data format.

Questions from questions table -

 [{'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id':  1, 'is_long_survey': 0}, 
    {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0}, 
    {'question_id': 3, 'question_text': 'Where is the infrastructure located?', 'category_id': 1, 'is_long_survey': 0}]"

Choices for these questions come from choice table with question_id as the foreign key.

[{'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
 {'choice_id' : 2, 'choice_text': '4', 'question_id': 1}, 
 {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
 {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
 {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
 {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
 {'choice_id' : 7, 'choice_text': 'local', 'question_id': 3}]

So each question has lets say 3 choices as shown below.

To display question text and their choices on the UI, how do I create a dictionary of question text as key and may be choices as multiple array values? Any other data structure I can use to format the data?

答案1

得分: 0

你可以像这样做:

qDicts = [ 
    {'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id':  1, 'is_long_survey': 0}, 
    {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0}, 
    {'question_id': 3, 'question_text': 'Where is the infrastructure located?', 'category_id': 1, 'is_long_survey': 0}  
]

chDicts = [
    {'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
    {'choice_id' : 2, 'choice_text': '4', 'question_id': 1}, 
    {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
    {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
    {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
    {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
    {'choice_id' : 7, 'choice_text': 'local', 'question_id': 3}
]

qustionChoices = {q['question_text']: [
    c['choice_text'] for c in chDicts
    if c['question_id']==q['question_id']
] for q in qDicts}

这使用了字典推导(用于构建qustionChoices字典本身)以及列表推导(用于为每个问题构建选择列表)。生成的输出(qustionChoices)应该如下所示:

{'How many servers run at max capacity?': ['2', '4', '6'],
 'Is the database getting replicated?': ['Yes', 'No'],
 'Where is the infrastructure located?': ['global', 'local']}

另外,你也可以将相关的选项嵌套到qDicts中,使其看起来像这样:

[{'question_id': 1,
  'question_text': 'How many servers run at max capacity?',
  'category_id': 1,
  'is_long_survey': 0,
  'choices': [
    {'choice_id': 1, 'choice_text': '2', 'question_id': 1},
    {'choice_id': 2, 'choice_text': '4', 'question_id': 1},
    {'choice_id': 3, 'choice_text': '6', 'question_id': 1}]},
 {'question_id': 2,
  'question_text': 'Is the database getting replicated?',
  'category_id': 1,
  'is_long_survey': 0,
  'choices': [
    {'choice_id': 4, 'choice_text': 'Yes', 'question_id': 2},
    {'choice_id': 5, 'choice_text': 'No', 'question_id': 2}]},
 {'question_id': 3,
  'question_text': 'Where is the infrastructure located?',
  'category_id': 1,
  'is_long_survey': 0,
  'choices': [
    {'choice_id': 6, 'choice_text': 'global', 'question_id': 3},
    {'choice_id': 7, 'choice_text': 'local', 'question_id': 3}]}
]

通过循环遍历qDicts(使用enumerate生成的索引)可以实现上述嵌套:

for qi, qd in enumerate(qDicts):
    qDicts[qi]['choices'] = [
        c for c in chDicts if c['question_id']==qd['question_id']]

或者,如果你更喜欢嵌套的字典在一个新的变量中(如下面的q_wCh),可以使用以下方式:

q_wCh = [{**q, 'choices':[
    c for c in chDicts if c['question_id']==q['question_id']
]} for q in qDicts]

在上述代码中,**q 用于解包每个qDicts中的字典。

英文:

You could do something like

qDicts = [ 
    {'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id':  1, 'is_long_survey': 0}, 
    {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0}, 
    {'question_id': 3, 'question_text': 'Where is the infrastructure located?', 'category_id': 1, 'is_long_survey': 0}  
] ## [ FROM YOUR EXAMPLE ] 
chDicts = [
    {'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
    {'choice_id' : 2, 'choice_text': '4', 'question_id': 1}, 
    {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
    {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
    {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
    {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
    {'choice_id' : 7, 'choice_text': 'local', 'question_id': 3}
] ## [ FROM YOUR EXAMPLE ] 


qustionChoices = {q['question_text']: [
    c['choice_text'] for c in chDicts
    if c['question_id']==q['question_id']
] for q in qDicts} ## [ OUTPUT ]

This uses dictionary comprehension (to form the qustionChoices dictionary itself) as well as list comprehension (to build a list of choices for each question). The resulting output (qustionChoices) should look like:

> python
> {'How many servers run at max capacity?': ['2', '4', '6'],
> 'Is the database getting replicated?': ['Yes', 'No'],
> 'Where is the infrastructure located?': ['global', 'local']}
>


You can also nest the relevant choices into qDicts so that it looks something like

> python
> [{'question_id': 1,
> 'question_text': 'How many servers run at max capacity?',
> 'category_id': 1,
> 'is_long_survey': 0,
> 'choices': [
> {'choice_id': 1, 'choice_text': '2', 'question_id': 1},
> {'choice_id': 2, 'choice_text': '4', 'question_id': 1},
> {'choice_id': 3, 'choice_text': '6', 'question_id': 1}] },
> {'question_id': 2,
> 'question_text': 'Is the database getting replicated?',
> 'category_id': 1,
> 'is_long_survey': 0,
> 'choices': [
> {'choice_id': 4, 'choice_text': 'Yes', 'question_id': 2},
> {'choice_id': 5, 'choice_text': 'No', 'question_id': 2}] },
> {'question_id': 3,
> 'question_text': 'Where is the infrastructure located?',
> 'category_id': 1,
> 'is_long_survey': 0,
> 'choices': [
> {'choice_id': 6, 'choice_text': 'global', 'question_id': 3},
> {'choice_id': 7, 'choice_text': 'local', 'question_id': 3}] } ]
>

by looping through it (with indices generated by enumerate) as below.

for qi, qd in enumerate(qDicts):
    qDicts[qi]['choices'] = [
        c for c in chDicts if c['question_id']==qd['question_id']]

or, if you'd prefer the nested dictionaries to be in a new variable (like q_wCh below),

q_wCh = [{**q, 'choices':[
    c for c in chDicts if c['question_id']==q['question_id']
]} for q in qDicts]

[ **q is used to unpack each dictionary q in qDicts ]

huangapple
  • 本文由 发表于 2023年2月10日 03:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定