[英]How do I format a nested python dictionary and write it to a json file?
[英]Python - how do I write pattern for nested dictionary?
我在一些运动中挣扎。 我必须创建一个嵌套字典,然后启动它。 我想(如果可能的话)使用 for 循环启动该字典。 问题是我不确定我应该如何声明某种模式。 我会尝试用一些例子来解释它。
假设我们有一个空字典
dictionary = {}
我想从中指定模式,例如
dictionary = {Name: {"Surname": surname, "Age": age}
在 for 循环中将启动“name”、“surname”、“age”等变量并添加到字典中
for x in range(0, 2):
name = input("Enter some name: ")
surname= input("Enter some surname: ")
age = int(input("Enter some shoe age: "))
我如何写下一些模式以在以后对其进行更新?
这可能会帮助您:
dictionary = {}
for _ in range(3):
name = input("Enter some name: ")
surname= input("Enter some surname: ")
age = int(input("Enter some shoe age: "))
dictionary[name] = {"Surname": surname, "Age": age}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.