社区所有版块导航
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 年前 • 1669 次点击  

我是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
 
1669 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Gustavo Sánchez
Reply   •   1 楼
Gustavo Sánchez    4 年前

这对熊猫来说很容易做到。您可以在他们的文档中查看有关语法的进一步解释,但代码如下:

import pandas as pd

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]
]

# Convert the data_list array into a DataFrame
df = pd.DataFrame(data_list, columns=['Name', 'Subject', 'Grade'])

# Group by Subject extracting the count and mean of the accumulated grade per subject
df.groupby('Subject').Grade.agg(['count', 'mean'])

enter image description here

Andrej Kesely
Reply   •   2 楼
Andrej Kesely    4 年前

itertools.groupby 分组 data_list 按主题:

from pprint import pprint
from statistics import mean
from itertools import groupby


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]
]

out = []
for k, g in groupby(sorted(data_list, key=lambda k: k[1]), lambda k: k[1]):
    g = [*g]
    out.append([k, len(g), mean(v[2] for v in g)])

pprint(out)

[['Algebra', 2, 7.5],
 ['Analysis', 3, 8.666666666666666],
 ['Biology', 1, 6],
 ['Math', 2, 8],
 ['PC', 4, 8.25],
 ['Physics', 3, 6]]