Re: Finding attributes in a list



> class Player(object):
> def __init__(self, **kw): self.__dict__.update(kw)
> def __repr__(self): return '<Player %s>'%getattr(self, 'name', '(anonymous)')
>
> import operator
> [p.name for p in sorted(players, key=operator.attrgetter('attacking'), reverse=True)]

Just happened to read this thread and wanted to say this is a neat
little example-- thank you! I have a couple of followup questions.

(1) Is there a performance penalty for using key=operator.attrgetter()?
(2) The Player class looks like a nice model for a data table when one
wants to sort by arbitrary column. Would you agree?
(3) Suppose one wished to construct a player list from a collection of
attribute lists, e.g.,

names = ['bob', 'sam', 'linda']
attack = [7, 5, 8]
defense = [6, 8, 6]
# construct players list here

Can you recommend an efficient way to construct the player list?

Thanks!
Marcus
.



Relevant Pages

  • Move VM to external hd after installing in internal?
    ... I have to reinstall it or can I copy the VM to my external then trash ... the VM on my internal hard drive so next time I start VPC 7 I can ... If someone could recommend the best method to do this I'd really ... Prev by Date: ...
    (microsoft.public.mac.virtualpc)
  • Re: WD data lifeguard tools on a HDD with data on it
    ... > Any reccomendations? ... With information you have supplied I cannot recommend any tools. ... make sure you backup ALL of the files you like to have back; ... Prev by Date: ...
    (comp.sys.ibm.pc.hardware.storage)
  • Re: Centralizers, Normalizers and the Center
    ... and they cyclic group of order 8 calculating each of these in ... order to get a better understanding of what they were. ... I recommend the ... Prev by Date: ...
    (sci.math)
  • Re: Is This Dangerous ?
    ... I think what they said was that they could not recommend it for use ... Pam in Bristol ... Prev by Date: ...
    (uk.rec.gardening)
  • RIS und Tastaturbelegung
    ... Layout hat? ... Danke. ... Marcus ... Prev by Date: ...
    (microsoft.public.de.german.windowsxp.setup)