It's a solution using Pandas.
However, the last average score output is unnecessarily complicated.
import pandas as pd
arr = [
["A", 80, 80, 80],
["A", 90, 90, 90],
["B", 60, 70, 80],
["A", 71, 78, 77],
["B", 70, 80, 73],
["B", 90, 87, 92],
]
print ("Original Data")
for ele in arr:
print(ele)
df = pd.DataFrame(arr, columns=["class", "score1", "score2", "score3"])
df = df.set_index("class")
df["average"] = df.mean(axis=1)
# # print(df)
listA = list(df[df.index == "A"]["average"])
listB = list(df[df.index == "B"]["average"])
print()
print ("listA, listB output")
# # print("A :", listA)
# # print("B :", listB)
print("A :", "[" + ", ".join([f"{e:.1f}" for e in listA]) + "]")
print("B :", "[" + ", ".join([f"{e:.1f}" for e in listB]) + "]")
© 2024 OneMinuteCode. All rights reserved.