我是新手,我的问题很简单。我有两个数组列表如下:
Grade = ["A","A-","B+","B","B-","C+","C","C-","D","F"]
GPA = [4,3.75,3.25,3,2.75,2.25,2,1.75,1,0]
如何从列表中指定A = 4,A- = 3.75等。
请帮助!!
答案 0 :(得分:5)
只需使用zip
功能:
grade_gpa = zip(Grade, GPA)
for pair in grade_gpa:
print(pair)
>>>
('A', 4)
('A-', 3.75)
('B+', 3.25)
('B', 3)
('B-', 2.75)
('C+', 2.25)
('C', 2)
('C-', 1.75)
('D', 1)
('F', 0)
此外,您可以使用zip对象构建字典:
grade_gpa_dict = dict(grade_gpa)
print(grade_gpa_dict['A'])
>>> 4
答案 1 :(得分:-1)
使用内置map / reduce:
import functools
a = map(lambda x,y : {x:y}, Grade, GPA)
b = functools.reduce(lambda x,y: dict(x.items() + y.items()), a)
b["A"] => 4