[Tutor] sorting / outputting varibales

Evert Rol evert.rol at gmail.com
Thu Nov 1 21:47:09 CET 2007


> Thanks, i think a dictionary may  be the way to go
>
>> in the end, you're going to use the
>> actual values anyway,
>> not their names, right?
>
> yeah, you're right, the way i was thinking about doing
> it probably won't work. The way I am doing it now is
> actually using attibutes of the var's themselves to
> determine how they get used in the function:
>
>       if var1.value > var2.value > var3.value:
>          objSprites = pygame.sprite.OrderedUpdates
> (var1, var2, var3)
>       elif var1.value > var3.value > var2.value:
>          objSprites = pygame.sprite.OrderedUpdates
> (var1, var3, var2)
>       elif var2.value > var3.value > var1.value:
>          objSprites = pygame.sprite.OrderedUpdates
> (var2, var3, var1)
>       elif var2.value > var1.value > var3.value:
>          objSprites = pygame.sprite.OrderedUpdates
> (var2, var1, var3)
>       elif var3.value > var1.value > var2.value:
>          objSprites = pygame.sprite.OrderedUpdates
> (var3, var1, var2)
>       elif var3.value > var2.value > var1.value:
>          objSprites = pygame.sprite.OrderedUpdates
> (var3, var2, var1)



Looks like you forgot to give one essential part of information in  
your first email: varX aren't 'simple' integers or floats, but more  
complex types. Not exactly what you gave in your example. Please do  
give all the information if you're not sure what you're asking about  
(it may even put people off in follow-up answering). Oh, and please  
reply to the whole list, not just me: it'll enlighten other people as  
well.

Anyway, with this, something like this (indeed a dictionary) would do  
it:

vars = [var1, var2, var3]
dictvars = {}
for var in vars:
   dictvars[var.value] = var
keys = dictvars.keys()
keys.sort()
sortedvars = [dictvars[key] for key in keys]
objSprites = pygame.sprite.OrderedUpdates(*sortedvars)


Arguably just as lengthly, but more consistent. Depending on the type  
of 'varX' variable you're using, you might be able to simplify things  
further (by using a dictionary instead of a home-made object, which I  
assume the 'varX' variables are).

Probably putting up a solution here isn't really "tutoring", but I'm  
curious if anyone else on the list has a cleaner solution than this.


Cheers,

   Evert



More information about the Tutor mailing list