0

I am looking to create a scoring system, and I am using prettytable to do so. I add all the scores to the table and then order them by their score. This works perfectly except for when I include their position (1, 2, 3, 4 etc.) If their position is included before the change, it changes so the number 1 is no longer at the top and 4 is no longer at the bottom, they're jumbled up.

Another method I've tried is adding the positions after sorting by score, but this adds the column to the right of the table, and I am aiming to have it on the right. If anyone could let me know how to

a) overwrite specific columns,
b) stop sorting affecting certain columns, or
c) add new columns to the left of the table,

it would be much appreciated.

from prettytable import PrettyTable

x = PrettyTable()
x.field_names = ["Position", "User", "Score"]


x.add_row([1, "Sam", 42])
x.add_row([2, "Ben", 43])
x.add_row([3, "Alex", 37])
x.add_row([4, "Joe", 54])

x.reversesort = True
y = x.sortby="Score"


print(x.get_string(start=0,end=4))

1 Answers1

0

It'd be better to separate printing from your scoring.

from prettytable import PrettyTable

# A dictionary mapping the user names to scores.

scores = {
    "Sam": 42,
    "Ben": 43,
    "Alex": 37,
    "Joe": 54,
}

# sorted() returns a sorted list from an iterable.
# Since `scores` is now a dict, we need to use `.items()`
# to get pairs out of it.
# The key function accesses the pair's value – the score.
# `reverse=True` is set for obvious reasons.

sorted_user_to_score = sorted(
    scores.items(),
    key=lambda pair: pair[1],
    reverse=True,
)

pt = PrettyTable()
pt.field_names = ["Position", "User", "Score"]

# enumerate() yields pairs of (position, datum) from an iterable.
# It accepts an optional start index parameter – we want positions
# to start from 1, not 0.

for position, (user, score) in enumerate(sorted_user_to_score, 1):
    pt.add_row([position, user, score])

print(pt.get_string(start=0, end=4))

AKX
  • 152,115
  • 15
  • 115
  • 172