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

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

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 -

  1. [{'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id': 1, 'is_long_survey': 0},
  2. {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0},
  3. {'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.

  1. [{'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
  2. {'choice_id' : 2, 'choice_text': '4', 'question_id': 1},
  3. {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
  4. {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
  5. {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
  6. {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
  7. {'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 -

  1. [{'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id': 1, 'is_long_survey': 0},
  2. {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0},
  3. {'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.

  1. [{'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
  2. {'choice_id' : 2, 'choice_text': '4', 'question_id': 1},
  3. {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
  4. {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
  5. {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
  6. {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
  7. {'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

你可以像这样做:

  1. qDicts = [
  2. {'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id': 1, 'is_long_survey': 0},
  3. {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0},
  4. {'question_id': 3, 'question_text': 'Where is the infrastructure located?', 'category_id': 1, 'is_long_survey': 0}
  5. ]
  6. chDicts = [
  7. {'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
  8. {'choice_id' : 2, 'choice_text': '4', 'question_id': 1},
  9. {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
  10. {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
  11. {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
  12. {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
  13. {'choice_id' : 7, 'choice_text': 'local', 'question_id': 3}
  14. ]
  15. qustionChoices = {q['question_text']: [
  16. c['choice_text'] for c in chDicts
  17. if c['question_id']==q['question_id']
  18. ] for q in qDicts}

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

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

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

  1. [{'question_id': 1,
  2. 'question_text': 'How many servers run at max capacity?',
  3. 'category_id': 1,
  4. 'is_long_survey': 0,
  5. 'choices': [
  6. {'choice_id': 1, 'choice_text': '2', 'question_id': 1},
  7. {'choice_id': 2, 'choice_text': '4', 'question_id': 1},
  8. {'choice_id': 3, 'choice_text': '6', 'question_id': 1}]},
  9. {'question_id': 2,
  10. 'question_text': 'Is the database getting replicated?',
  11. 'category_id': 1,
  12. 'is_long_survey': 0,
  13. 'choices': [
  14. {'choice_id': 4, 'choice_text': 'Yes', 'question_id': 2},
  15. {'choice_id': 5, 'choice_text': 'No', 'question_id': 2}]},
  16. {'question_id': 3,
  17. 'question_text': 'Where is the infrastructure located?',
  18. 'category_id': 1,
  19. 'is_long_survey': 0,
  20. 'choices': [
  21. {'choice_id': 6, 'choice_text': 'global', 'question_id': 3},
  22. {'choice_id': 7, 'choice_text': 'local', 'question_id': 3}]}
  23. ]

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

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

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

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

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

英文:

You could do something like

  1. qDicts = [
  2. {'question_id': 1, 'question_text': 'How many servers run at max capacity?', 'category_id': 1, 'is_long_survey': 0},
  3. {'question_id': 2, 'question_text': 'Is the database getting replicated?', 'category_id': 1, 'is_long_survey': 0},
  4. {'question_id': 3, 'question_text': 'Where is the infrastructure located?', 'category_id': 1, 'is_long_survey': 0}
  5. ] ## [ FROM YOUR EXAMPLE ]
  6. chDicts = [
  7. {'choice_id' : 1, 'choice_text': '2', 'question_id': 1},
  8. {'choice_id' : 2, 'choice_text': '4', 'question_id': 1},
  9. {'choice_id' : 3, 'choice_text': '6', 'question_id': 1},
  10. {'choice_id' : 4, 'choice_text': 'Yes', 'question_id': 2},
  11. {'choice_id' : 5, 'choice_text': 'No', 'question_id': 2},
  12. {'choice_id' : 6, 'choice_text': 'global', 'question_id': 3},
  13. {'choice_id' : 7, 'choice_text': 'local', 'question_id': 3}
  14. ] ## [ FROM YOUR EXAMPLE ]
  15. qustionChoices = {q['question_text']: [
  16. c['choice_text'] for c in chDicts
  17. if c['question_id']==q['question_id']
  18. ] 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.

  1. for qi, qd in enumerate(qDicts):
  2. qDicts[qi]['choices'] = [
  3. 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),

  1. q_wCh = [{**q, 'choices':[
  2. c for c in chDicts if c['question_id']==q['question_id']
  3. ]} 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:

确定