[英]how to extract a dictionary from a data structure using python?
so here is the table, named grade_lists, that maps names of students to lists of their exam grades. 因此,这是一个名为grade_lists的表,该表将学生的姓名映射到他们的考试成绩列表。 The grades should be converted from strings to integers.
等级应从字符串转换为整数。 For instance, grade_lists['Thorny'] == [100, 90, 80].
例如,grade_lists ['Thorny'] == [100,90,80]。 can someone help me?
有人能帮我吗?
grades = [ #here is the table
['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
['Thorny', '100', '90', '80'],
['Mac', '88', '99', '111'],
['Farva', '45', '56', '67'],
['Rabbit', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Foster', '89', '97', '101']
]
Just iterate through the list with dict
: 只需使用
dict
遍历列表即可:
grades_dict = dict((x[0],x[1:]) for x in grades[1:])
grades_dict
Output: 输出:
{'Farva': ['45', '56', '67'],
'Foster': ['89', '97', '101'],
'Mac': ['88', '99', '111'],
'Rabbit': ['59', '61', '67'],
'Thorny': ['100', '90', '80'],
'Ursula': ['73', '79', '83']}
and grades_dict['Thorny']
: 和
grades_dict['Thorny']
:
['100', '90', '80']
Use a dictionary-comprehension that maps values to integer: 使用将值映射到整数的字典理解:
{x[0]: list(map(int, x[1:])) for x in grades[1:]}
Example : 范例 :
grades = [ #here is the table
['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
['Thorny', '100', '90', '80'],
['Mac', '88', '99', '111'],
['Farva', '45', '56', '67'],
['Rabbit', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Foster', '89', '97', '101']
]
print({x[0]: list(map(int, x[1:])) for x in grades[1:]})
# {'Thorny': [100, 90, 80],
# 'Mac': [88, 99, 111],
# 'Farva': [45, 56, 67],
# 'Rabbit': [59, 61, 67],
# 'Ursula': [73, 79, 83],
# 'Foster': [89, 97, 101]}
This outputs a dictionary with name as key and list of marks each converted to integer as value of that key. 这将输出一个字典,其名称为键,而标记列表则分别转换为整数作为键值。
Just run this block of code. 只需运行这段代码。
grades = [ #here is the table
['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
['Thorny', '100', '90', '80'],
['Mac', '88', '99', '111'],
['Farva', '45', '56', '67'],
['Rabbit', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Foster', '89', '97', '101']
]
dict = {}
for student_data in grades:
if student_data[0] == "Student":
continue
else:
temp_list = []
temp_list.append(int(student_data[1]))
temp_list.append(int(student_data[2]))
temp_list.append(int(student_data[3]))
dict[student_data[0]] = temp_list
print(dict)
解决上述问题的一种简单方法:
grades_dict = dict(map(lambda x: (x[0], x[1:]), grades))
Instead of iterating though list and then converting it to dictionary, you can directly use dictionary comprehension. 您可以直接使用字典理解,而无需遍历列表然后将其转换为字典。 It's more Pythonic.
它更像Python。
grades = [ #here is the table
['Student', 'Exam_1', 'Exam_2', 'Exam_3'],
['Thorny', '100', '90', '80'],
['Mac', '88', '99', '111'],
['Farva', '45', '56', '67'],
['Rabbit', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Foster', '89', '97', '101']
]
grade = {g[0]:g[1:] for g in grades[1:]}
print(grade)
# Output
#{'Thorny': ['100', '90', '80'], 'Mac': ['88', '99', '111'], 'Ursula': ['73', '79', '83'], 'Farva': ['45', '56', '67'], 'Foster': ['89', '97', '101'], 'Rabbit': ['59', '61', '67']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.