Я хочу изменить структуру словаря - PullRequest
0 голосов
/ 22 марта 2020

Я пытаюсь изменить формат моего словаря из списка значений на диктовку значений, где значения также имеют ключи от нуля до бесконечности. Я надеюсь, что приведенный ниже пример поможет лучше понять.

У меня есть словарь в этом формате:

{
  "Where did you eat today?": [
    "Waakye",
    "Banku",
    "Banku",
    "Banku"
  ],
  "What is your name?**": [
    "Benjamin",
    "Kwabena James",
    "Benje",
    "Benard"
  ],
  "Which of the following countries was COVID-19 first realized?": [
    "Jamaica",
    "China",
    "China",
    "NaN"
  ],
  "The proposed cocoa insurance scheme is not a good initiative": [
    "Strongly Agree",
    "Strongly Agree",
    "NaN",
    "NaN"
  ],
  "The cocoa insurance scheme will not effectively mitigate the risks cocoa farmers face": [
    "Stringly Disagree",
    "Neutral",
    "NaN",
    "NaN"
  ],
  "The scheme is not in the interest of cocoa farmers": [
    "Stringly Disagree",
    "Disagree",
    "NaN",
    "NaN"
  ],
  "A scheme like that is not worth contributing to": [
    "Stringly Disagree",
    "Stringly Disagree",
    "NaN",
    "NaN"
  ],
  "Cocoa insurance is likely to be expensive": [
    "Neutral",
    "Agree",
    "NaN",
    "NaN"
  ],
  "Which of the following did Bill Gates graduate from": [
    "Hazard",
    "NaN",
    "NaN",
    "NaN"
  ],
  "Which of the following do you plan on owing inn 2021?": [
    "Business",
    "NaN",
    "NaN",
    "NaN"
  ]
}

, и я хочу изменить формат на этот:

{
  "How many of you are attending?": {
    "0": null,
    "1": null,
    "2": 5
  },
  "The proposed cocoa insurance scheme is not a good initiative [The proposed cocoa insurance scheme is not a good initiative]": {
    "0": "5",
    "1": "Strongly Disagree",
    "2": "Strongly Agree"
  },
  "The proposed cocoa insurance scheme is not a good initiative [The cocoa insurance scheme will not effectively mitigate the risks cocoa farmers face]": {
    "0": null,
    "1": "Neutral",
    "2": "Agree"
  },
  "The proposed cocoa insurance scheme is not a good initiative [The scheme is not in the interest of the cocoa farmers]": {
    "0": null,
    "1": "Strongly Agree",
    "2": "Neutral"
  },
  "The proposed cocoa insurance scheme is not a good initiative [A scheme like that is not worth contributing to]": {
    "0": null,
    "1": "Agree",
    "2": "Disagree"
  },
  "The proposed cocoa insurance scheme is not a good initiative [The government is using this initiative to get money from cocoa farmers]": {
    "0": null,
    "1": "Disagree",
    "2": "Strongly Disagree"
  },
  "The proposed cocoa insurance scheme is not a good initiative [Cocoa insurance is likely to be expensive ]": {
    "0": null,
    "1": "Neutral",
    "2": "Disagree"
  },
  "The proposed cocoa insurance scheme is not a good initiative [The insurance companies may delay to compensate cocoa farmers when the risk occurs]": {
    "0": null,
    "1": "Strongly Agree",
    "2": "Neutral"
  },
  "The proposed cocoa insurance scheme is not a good initiative [The insurance companies may refuse to pay cocoa farmers when they are supposed to]": {
    "0": null,
    "1": null,
    "2": "Agree"
  },
  "Can you attend?": {
    "0": "Yes,  I'll be there",
    "1": "Sorry, can't make it",
    "2": "Sorry, can't make it"
  },
  "What is your name?": {
    "0": null,
    "1": null,
    "2": "benjelino"
  },
  "What will you be bringing?": {
    "0": null,
    "1": null,
    "2": "Mains;Salad;Dessert;Drinks;Sides/Appetizers;Banku"
  },
  "What is your email address?": {
    "0": null,
    "1": null,
    "2": "gh@kkk.com"
  },
  "Do you have any allergies or dietary restrictions?": {
    "0": null,
    "1": null,
    "2": "llm;k"
  },
  "jn;plm;": {
    "0": null,
    "1": null,
    "2": "jkjnlkj"
  }
}

поэтому вместо списка значений я хочу указать значения, как и второй код.

1 Ответ

0 голосов
/ 22 марта 2020
new_dict = {}
for key, value in old_dict.items():
    new_dict[key] = dict(enumerate(value))

enumerate создает итератор для каждого списка.
при преобразовании в dict (с dict) создает словарь, в котором ключи являются индексами, а значения являются элементами списка.

...