简体   繁体   English

用另一个列表中的项目替换列表中的项目

[英]Replacing items in a list with items from another list

I'm trying to create a calculator for a User's weighted GPA.我正在尝试为用户的加权 GPA 创建一个计算器。 I'm using PyautoGUI to ask the user for their grades and type of class they're taking.我正在使用 PyautoGUI 向用户询问他们的成绩和他们正在上课的课程类型。 But I want to be able to take that User input and essentially remap it to a different value.但我希望能够接受该用户输入并将其重新映射到不同的值。

class GPA():
    grades = []
    classtypes = []

    your_format = confirm(text='Choose your grade format: ', title='', 
    buttons=['LETTERS', 'PERCENTAGE', 'QUIT'])

    classnum = int(prompt("Enter the number of classes you have: "))

    for i in range(classnum):
        grade = prompt(text='Enter your grade for the course 
:'.format(name)).lower()
    classtype = prompt(text='Enter the type of Course (Ex. Regular, AP, Honors): ').lower()

    classtypes.append(classtype)
    grades.append(grade)

    def __init__(self):
        self.gradeMap = {'a+': 4.0, 'a': 4.0, 'a-': 3.7, 'b+': 3.3, 'b': 3.0,'b-': 2.7,
         'c+': 2.3, 'c': 2.0, 'c-': 1.7, 'd+': 1.3, 'd': 1.0, 'f': 0.0}
        self.weightMap = {'advanced placement': 1.0, 'ap': 1.0, 'honors': 0.5,'regular': 0.0}

Based on the gradeMap dictionary you have defined you could do something with what's called a list comprehension .根据您定义的gradeMap字典,您可以使用所谓的list comprehension做一些事情。

An example of what I'm talking about done using the Python interpreter:我所说的使用 Python 解释器完成的示例:

>>> grades = ['a', 'c-', 'c']
>>> gradeMap = {'a+': 4.0, 'a': 4.0, 'a-': 3.7, 'b+': 3.3, 'b': 3.0,'b-': 2.7,
...             'c+': 2.3, 'c': 2.0, 'c-': 1.7, 'd+': 1.3, 'd': 1.0, 'f': 0.0}
>>> [gradeMap[grade] for grade in grades] #here's the list comprehension
[4.0, 1.7, 2.0]

I think the downside with this approach might be making sure the user only gives you a grade you have defined in your gradeMap otherwise it is going to give you a KeyError .我认为这种方法的缺点可能是确保用户只给你一个你在gradeMap定义的gradeMap否则它会给你一个KeyError

Another alternative would be to use map .另一种选择是使用map map is slightly different in that it expects a function and an input list, and then applys that function over the input list. map稍有不同,它需要一个函数和一个输入列表,然后将该函数应用于输入列表。

An example with a very simple function that only works with a few grades:一个非常简单的函数的例子,它只适用于几个等级:

>>> def convert_grade_to_points(grade):
...   if grade == 'a':
...     return 4.0
...   elif grade == 'b':
...     return 3.0
...   else:
...     return 0
... 
>>> grades = ['a', 'b', 'b']
>>> map(convert_grade_to_points, grades)
[4.0, 3.0, 3.0]

This also suffers from the downside I mentioned earlier that the function you define has to handle the case where the user input an invalid grade.这也有我之前提到的缺点,即您定义的函数必须处理用户输入无效成绩的情况。

You can replace items of the list in place.您可以就地替换列表中的项目。

for grade in gradeList:
    if type is "PERCENTAGE":
       grade = grade × some_factor  # use your logic
    elif type is "LETTERS":
       grade="some other logic"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM