如何以人名和成绩返回平均成绩?

时间:2017-10-04 17:10:17

标签: python python-3.x

def average_grade(L):
    '''(list of list) -> list
    L is a list of lists of strings. The first item in each
    sublist is a student name and the rest of the strings in the 
    sublist are grade values (numeric). Return a 
    new list of lists where each sublist has length 2 of
    the form [student_name (str), average grade (float)]
    >>> average_grade([['Bob', 56, 80, 72, 90], ['Alice', 60, 88, 44, 70], ['Joe', 44, 100, 80, 60, 50]])
    [['Bob', 74.5], ['Alice', 65.5], ['Joe', 66.8]]
    '''
    new_list = [[]]
    for i in range(len(L)):
        if type(item) == type('a'):
            new_list.append(L)
            if type(item) == type(50):
                new_list.append(sum(L)/len(L))
    return new_list

我有这个,它不起作用,但我不知道如何解决它。有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:0)

你误解了列表的工作方式。每次从L循环时都需要创建临时列表,并在外部列表中添加此临时列表。 这是您的代码的实现。

def average_grade(L):
    '''(list of list) -> list
    L is a list of lists of strings. The first item in each
    sublist is a student name and the rest of the strings in the 
    sublist are grade values (numeric). Return a 
    new list of lists where each sublist has length 2 of
    the form [student_name (str), average grade (float)]
    >>> average_grade([['Bob', 56, 80, 72, 90], ['Alice', 60, 88, 44, 70], ['Joe', 44, 100, 80, 60, 50]])
    [['Bob', 74.5], ['Alice', 65.5], ['Joe', 66.8]]
    '''
    # Output list.
    new_list = []
    number_of_subjects = float(len(L[0][1:]))
    for lis in L:
        name = lis[0]
        # Using / instead of // to avoid generating integer results
        average = float(sum(lis[1:])) / number_of_subjects
        new_list.append([name, average])
    return new_list

答案 1 :(得分:0)

试试这个。

public static void main(String[] args) throws ParseException, ClassNotFoundException, IllegalAccessException {
    Scanner sc = new Scanner(System.in);
    System.out.println("\nEnter your color\n" +
            "BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
    List<Color> colorArray= new ArrayList<>();
    Class<Color> colorClass = Color.class;
    while(sc.hasNext()) {
        String next = sc.next();
        try {
            Color c = colorClass.cast(colorClass.getField(next.toLowerCase()).get(null));
            colorArray.add(c);
            System.out.printf("Added %s%n", c);
        } catch (NoSuchFieldException e) {
            if("END".equals(next)) {
                break;
            }
            System.err.printf("Sorry, could not find %s%n", next);
        }
    }
    System.out.println(colorArray);
}

答案 2 :(得分:0)

您的示例输入不是字符串列表的列表。每个子列表包含一个字符串,其余为数字。您的代码由于多种原因而无法正常工作,包括item未定义,将new_list附加到函数的整个输入,然后继续将L视为def average_grade(L): new_list = [] for l in L: name = l[0] grades = [float(val) for val in l[1:]] # Now works when grades are strings or numerics average = sum(grades)/len(grades) new_list.append([name, average]) return new_list 它是一个子列表,而不是整个列表。如果这对您来说很重要,我建议您回过头来评估代码的每一行;例如,在每行之后打印几个变量。无论如何:

<SeekBar
            android:id="@+id/opacitybar"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:max="10"
            android:progress="10"
            android:layout_alignTop="@+id/opacitylbl"
            android:layout_centerHorizontal="true" />

答案 3 :(得分:0)

如果您使用的是Python3.3 +。您也可以将其更改为列表理解。

def average_grade(grade_book):
    return [
        [name, sum(grades)/len(grades)]
        for name, *grades in grade_book
    ]