1

我是新手,我的问题很简单。我有两个数组列表,如下所示:

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 等等。

请帮忙!!

4

2 回答 2

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
于 2013-02-05T10:40:22.460 回答
-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
于 2013-02-05T12:35:26.127 回答