I want to know how to print out the elements in the Python list and calculate them.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

It's a problem.

Source list output, one element output of arr in a line

Create List

Store the obtained mean in listA, or listB according to half

Output listA and listB

my coding.

The price should be as below!

python list

2022-09-20 10:47

1 Answers

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]) + "]")


2022-09-20 10:47

If you have any answers or tips


© 2022 pinfo. All rights reserved.