社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python排序学生课程的嵌套列表

Nontas • 4 年前 • 1747 次点击  

我是python新手,我很难完成这段代码。有了数据表和给定的代码,我如何制作一个代码来创建每个科目的嵌套列表,参加人数,该科目的平均成绩?输出应如下所示:

[["Physics", 3, 6.0], ["PC", 4, 8.25], ....]

data_list =
 ["John", "Physics", 5], ["John", "PC", 7], ["John", "Math", 8], 
 ["Mary", "Physics", 6], ["Mary", "PC", 10], ["Mary", "Algebra", 7], 
 ["Helen", "Physics", 7], ["Helen","PC", 6], ["Helen", "Algebra", 8], 
 ["Helen", "Analysis", 10], ["Bill", "PC", 10], ["Bill", "Analysis", 6], 
 ["Bill", "Math", 8], ["Bill", "Biology", 6], ["Michael", "Analysis", 10]
]

def groupby(data, index, category):
    """Sort list of records by index and category
    """
    output = []
    indices = []
    for record in data:
        if record[index] not in indices:
            indices.append(record[index])
            output.append([record[index]])
            output[-1].append(record[category])
        else:
            output[indices.index(record[index])].append(record[category])
    return output

# index 0 -> person
# category 1 -> subject
subject_list = groupby(data_list, 0, 1)

# index 0 -> person
# category 2 -> grade
grade_list = groupby(data_list, 0, 2)

# grad_list
[['John', 5, 7, 8],
 ['Mary', 6, 10, 7],
 ['Helen', 7, 6, 8, 10],
 ['Bill', 10, 6, 8, 6],
 ['Michael', 10]]

然后,你可以得到每人所学科目的数量或平均成绩,如下所示:

import statistics

subjects_taken = [len(x) - 1 for x in subject_list]
average_grade = [statistics.mean(x[1:]) for x in grade_list]

persons = [x[0] for x in subject_list]
final_list = list(zip(persons, subjects_taken, average_grade))

# final_list
[('John', 3, 6.666666666666667),
 ('Mary', 3, 7.666666666666667),
 ('Helen', 4, 7.75),
 ('Bill', 4, 7.5),
 ('Michael', 1, 10)]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/99357
 
1747 次点击  
文章 [ 2 ]  |  最新文章 4 年前