【问题标题】:Creating a dictionary inside another dictionary在另一个字典中创建一个字典
【发布时间】:2021-03-09 18:37:52
【问题描述】:

鉴于以下数据,我如何创建一个字典,其中键是学生的姓名,值是字典,键是测试,值是他们在其中得到的成绩。

grades =  [
   ['Students', 'Test 1', 'Test 2', 'Test 3'],
   ['Tomas', '100', '90', '80'],
   ['Marcos', '88', '99', '111'],
   ['Flavia', '45', '56', '67'],
   ['Ramon', '59', '61', '67'],
   ['Ursula', '73', '79', '83'],
   ['Federico', '89', '97', '101']
] 

我试过这样做,但我不知道为什么它没有正确显示成绩。

notas_dict={}

def dic(etiquets, notas):
   for i in range(len(etiquets)):
       notas_dict[etiquets[i]]=int(notas[i])
   return notas_dict
dic(['Test 1','Test 2', 'Test 3'], ['100','80','90'] )

dic_final={}

for line in grades[1:]:
   line_grades=[int(element) for element in line[1:]]
   dic_final[line[0]]=dic(['Test 1','Test 2', 'Test 3'], line_grades)
print(dic_final)

输出应该是:



{'Tomas': {'Test 1': 100, 'Test 2': 90, 'Test 3': 80}, 'Marcos': {'Test 1': 88, 'Test 2': 99, 'Test 3': 111}, 'Flavia': {'Test 1': 45, 'Test 2': 56, 'Test 3': 67}, 'Ramon': {'Test 1': 59, 'Test 2': 61, 'Test 3': 67}, 'Ursula': {'Test 1': 73, 'Test 2': 79, 'Test 3': 83}, 'Federico': {'Test 1': 89, 'Test 2': 97, 'Test 3': 101}}

【问题讨论】:

  • dic 是什么?你没有在任何地方定义它。另外,你得到什么输出?
  • dic 是我创建的一个函数,用于创建将作为值的字典(我的错误是我忘了放代码,但我编辑了它)。我得到的输出是:{'Tomas': {'Control 1': 89, 'Control 2': 97, 'Control 3': 101}, 'Marcos': {'Control 1': 89, 'Control 2': 97, 'Control 3': 101}, 'Flavia': {'Control 1': 89, 'Control 2': 97, 'Control 3': 101}, 'Ramon': {'Control 1': 89, 'Control 2': 97, 'Control 3': 101}, 'Ursula': {'Control 1': 89, 'Control 2': 97, 'Control 3': 101}, 'Federico': {'Control 1': 89, 'Control 2': 97, 'Control 3': 101}}

标签: python-3.x dictionary for-loop


【解决方案1】:

你可以使用:

{i[0]:dict(zip(grades[0][1:],i[1:])) for i in grades[1:]}

结果:

{'Tomas': {'Test 1': '100', 'Test 2': '90', 'Test 3': '80'},
 'Marcos': {'Test 1': '88', 'Test 2': '99', 'Test 3': '111'},
 'Flavia': {'Test 1': '45', 'Test 2': '56', 'Test 3': '67'},
 'Ramon': {'Test 1': '59', 'Test 2': '61', 'Test 3': '67'},
 'Ursula': {'Test 1': '73', 'Test 2': '79', 'Test 3': '83'},
 'Federico': {'Test 1': '89', 'Test 2': '97', 'Test 3': '101'}}

如果你想得到 int 的成绩:

{i[0]:dict(zip(grades[0][1:],list(map(int,i[1:])))) for i in grades[1:]}

【讨论】:

  • 有没有办法以整数形式获得成绩?
  • 更新了!您可以将 int 函数映射到整个列表:{i[0]:dict(zip(grades[0][1:],list(map(int,i[1:])))) for i in grades[1:]}
【解决方案2】:

创建一个数据框,然后使用 to_records 创建一个元组列表,其中每个元组是一行。然后,您可以按索引对元组进行切片。

 grades =  [
   ['Students', 'Test 1', 'Test 2', 'Test 3'],
   ['Tomas', '100', '90', '80'],
   ['Marcos', '88', '99', '111'],
   ['Flavia', '45', '56', '67'],
   ['Ramon', '59', '61', '67'],
  ['Ursula', '73', '79', '83'],
  ['Federico', '89', '97', '101']
 ] 

 Columns=grades[0]
 df=pd.DataFrame(columns=Columns)
 for i in range(1, len(grades)):
     df_length = len(df)
     df.loc[df_length] = grades[i]

 print(df.to_records())

输出:

 [(0, 'Tomas', '100', '90', '80') (1, 'Marcos', '88', '99', '111')
 (2, 'Flavia', '45', '56', '67') (3, 'Ramon', '59', '61', '67')
 (4, 'Ursula', '73', '79', '83') (5, 'Federico', '89', '97', '101')]

 dict=df.T.to_dict()
 for k,v in dict.items():
     print(k,v['Students'],v['Test1'],v['Test2'],v['Test3'])

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2021-06-28
    • 2014-06-17
    • 1970-01-01
    相关资源
    最近更新 更多