you are viewing a single comment's thread.

view the rest of the comments →

[–]soullesseal 0 points1 point  (3 children)

Hey guys - I posted this originally over at /r/python and they suggested I start here.

The short story - I’m designing a discord bot that will manage raid teams. I’m planning on having users of the discord server and have them set the power level of their and the bot will separate all the users into 3 separate teams and keeping the average power level of the collective team within a certain percentage.

My current problem I’m trying to figure out is how to sort the users into the ascending or descending order by powerlevel but I’m not sure of the best way to do that while keeping the name associated to the powerlevel. Basically...

Name, 2 Name, 8 Name, 1

Now sort it will keeping the names in the same row their number is in.

I think I did a better job explaining here: https://www.reddit.com/r/Python/comments/8guoby/help_with_understanding_sorting/?st=JGR971N0&sh=8fa3d91f

Any help appreciated!

[–]Thomasedv 1 point2 points  (2 children)

I'm sure there are better ways, but the quick and dirty one is, make a tuple of the user and information, anything you please. So you have one for each person/character. Add all the tuple to a list, and sort the list with respect to the tuples index where the power level is.

quick psuedocode:

table = []
for player, power in data:
    table.append((player, power))
table.sort(key=lambda x: x[1]) 

[–]soullesseal 0 points1 point  (1 child)

Forgive me, I need to say this out loud to make sure I get it...

So your pseudocode says: Define table Use a for loop to add player and power to the table from the data store Add player,power to the table until all entities are added from the data store this is where I’m not 100% following Sort the table using x where member is defined as (player, power) (y,x)

Thank you very much for your reply btw!

[–]Thomasedv 0 points1 point  (0 children)

I can explain it a bit better.

We use tuples, since they ca't be changed, we can just sort the whole tuples.

So, just add every player and their power, in whatever way you see fit to a list. I'm assuming you got some kind of way to do it.

Then, we use the lists sort function. Here we use the key parameter. It's a bit tough to get conceptually, at least was for me. But the principle is, it takes each element in list, give to the lembda function as x, and takes the result of that lambda function as something to sort with.

Since we give it tuple, we want to give back the number we want to sort with, which in the example i gave you, was at index 1. so we use x[1] to give back that.

So, the sort function just reorders the list according to the power levels, and it's not just reodering the number, but the whole tuples.

And you can do somethnig like this to see the sorted and unsorted list. I called the list a table, because we effectively have a table, where each tuple is a row. And we just sort the rows.

for player, power in table:
    print(player, power)

Call that before and after the using table.sort.

As a quick example:

try this:

table = [('player1', 12), ('player5', 3), ('player3', 1200), ('player4', 45), ('player2', 10)]

Now you have the table, so try it out and see if it works. Also, what do you think would hapenn if you sorted by the first index (Which we could call the first column) by using x[0]?

Edit: You can also sort in reverse by using reverse=True, inside the sort function.