# 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

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

Popular Tags
python x 1522
android x 362
c x 361
java x 353
javascript x 303
php x 126
swift x 112
mysql x 92
r x 87
html x 51