0

I have a python script which produces this output:

[(22, 8, 40.0), (23, 1, 5.0), (24, 1, 5.0), ('Friday', 2, 10.0), ('Monday', 4, 20.0), ('Thursday', 2, 10.0), ('Tuesday', 1, 5.0), ('Wednesday', 1, 5.0)]

How can I get Python to show me this in a table format with the headings : Value, Count, Percentage

Additionally, I have not managed to get sorting to work. I want it to have the highest percentage value at the top of the list descending order.

My script for the sort was # sort results sorted(rank_values_perc, key=lambda x: x[2], reverse=True)

1 Answer 1

2

Use tabulate:

from tabulate import tabulate

data = [(22, 8, 40.0), (23, 1, 5.0), (24, 1, 5.0), ('Friday', 2, 10.0), ('Monday', 4, 20.0), ('Thursday', 2, 10.0), ('Tuesday', 1, 5.0), ('Wednesday', 1, 5.0)]

header = ['Value', 'Count', 'Percentage']
data = sorted(data, key=lambda x: x[2], reverse=True)
print(tabulate(data, headers=header))

You can refer to this link for more options: Printing Lists as Tabular Data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.