Re: Get list of attributes from list of objects?

2017-08-02 Thread Ian Kelly
On Wed, Aug 2, 2017 at 11:49 AM, Chris Angelico wrote: > On Thu, Aug 3, 2017 at 3:21 AM, Ian Pilcher wrote: >> Given a list of objects that all have a particular attribute, is there >> a simple way to get a list of those attributes? >> >> In other words: >> >

Re: Get list of attributes from list of objects?

2017-08-02 Thread Ian Pilcher
On 08/02/2017 12:49 PM, Chris Angelico wrote: On Thu, Aug 3, 2017 at 3:21 AM, Ian Pilcher wrote: You can't eliminate the loop, but you can compact it into a single logical operation: namelist = [foo.name for foo in foolist] That's a "list comprehension", and is an elegant way to process a list

Re: Get list of attributes from list of objects?

2017-08-02 Thread Terry Reedy
On 8/2/2017 1:21 PM, Ian Pilcher wrote: Given a list of objects that all have a particular attribute, is there a simple way to get a list of those attributes? In other words: class Foo(object): def __init__(self, name): self.name = name foolist = [ Foo('a'

Re: Get list of attributes from list of objects?

2017-08-02 Thread Chris Angelico
On Thu, Aug 3, 2017 at 3:21 AM, Ian Pilcher wrote: > Given a list of objects that all have a particular attribute, is there > a simple way to get a list of those attributes? > > In other words: > > class Foo(object): > def __init__(self, name): > self.n

Get list of attributes from list of objects?

2017-08-02 Thread Ian Pilcher
Given a list of objects that all have a particular attribute, is there a simple way to get a list of those attributes? In other words: class Foo(object): def __init__(self, name): self.name = name foolist = [ Foo('a'), Foo('b'), Foo('c') ]

Re: Object containing a list of objects.

2010-08-28 Thread cocolombo
Chris I take good notice of your comments and suggestions. Thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: Object containing a list of objects.

2010-08-28 Thread cocolombo
Thanks MRAB and Peter Otten that solved the problem. -- http://mail.python.org/mailman/listinfo/python-list

Re: Object containing a list of objects.

2010-08-28 Thread Chris Rebert
On Sat, Aug 28, 2010 at 10:48 AM, cocolombo wrote: > Hello. > > I am putting objects (test) into a container object (tests) and the > test object is also a container for a another list of object > (scores). > > Problem is that all instances of class tests have the same value. > > To illustrate: >

Re: Object containing a list of objects.

2010-08-28 Thread Peter Otten
cocolombo wrote: > Problem is that all instances of class tests have the same value. > > To illustrate: > class tests(object): > listOfTest = [] This makes listOfTest a class attribute. To get one list per instance define it in the initializer: class Tests(object): def __init__(self):

Re: Object containing a list of objects.

2010-08-28 Thread MRAB
On 28/08/2010 18:48, cocolombo wrote: Hello. I am putting objects (test) into a container object (tests) and the test object is also a container for a another list of object (scores). Problem is that all instances of class tests have the same value. To illustrate: class score(object): va

Object containing a list of objects.

2010-08-28 Thread cocolombo
Hello. I am putting objects (test) into a container object (tests) and the test object is also a container for a another list of object (scores). Problem is that all instances of class tests have the same value. To illustrate: class score(object): val = 0 def __init__(self, val):

Re: self-aware list of objects able to sense constituent member alterations?

2009-02-08 Thread John O'Hagan
On Fri, 6 Feb 2009, greyw...@gmail.com wrote: > On Jan 28, 4:37 am, John O'Hagan wrote: > > On Tue, 27 Jan 2009, Reckoner wrote: > > > I'm not sure this is possible, but I would like to have > > > a list of  objects > > > > > > A=[a,b,

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:41 pm, Stephen Hansen wrote: > > I think there may have been a misunderstanding.  I was already using > > attrgetter, my problem is that it doesn't appear to be sorting by the > > argument i give it.  How does sort work with strings?  How about with > > datetime.time or datetime.date?

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Stephen Hansen
> I think there may have been a misunderstanding. I was already using > attrgetter, my problem is that it doesn't appear to be sorting by the > argument i give it. How does sort work with strings? How about with > datetime.time or datetime.date? You were using the attrgetter, but it looks like

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:34 pm, Robocop wrote: > On Feb 6, 2:20 pm, Robocop wrote: > > > > > On Feb 6, 2:17 pm, Robocop wrote: > > > > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > > Robocop: > > > > > >then within each department block of the list, have it organized by > > > > >projects.< > >

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Terry Reedy
Robocop wrote: UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter("department", "engagement", "date","start")) TypeError: attrgetter expected 1 arguments, got 4 Um... what version of Python are you running? Alway specify. (Too many people do not). In 3.0 from operator imp

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:20 pm, Robocop wrote: > On Feb 6, 2:17 pm, Robocop wrote: > > > > > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > Robocop: > > > > >then within each department block of the list, have it organized by > > > >projects.< > > > > I don't know what does it means. > > > > > ti

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 2:17 pm, Robocop wrote: > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > > Robocop: > > > >then within each department block of the list, have it organized by > > >projects.< > > > I don't know what does it means. > > > > timesheets.sort(key=operator.attrgetter('string')) >

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > Robocop: > > >then within each department block of the list, have it organized by > >projects.< > > I don't know what does it means. > > > timesheets.sort(key=operator.attrgetter('string')) > > Try something like: > timesheets.sort(key=attrgette

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > Robocop: > > >then within each department block of the list, have it organized by > >projects.< > > I don't know what does it means. > > > timesheets.sort(key=operator.attrgetter('string')) > > Try something like: > timesheets.sort(key=attrgette

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread bearophileHUGS
Robocop: >then within each department block of the list, have it organized by projects.< I don't know what does it means. > timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter("department", "engagement", "date", "stare_hour")) > My brain might

Re: Trouble sorting a list of objects by attributes

2009-02-06 Thread Miki
> I've found myself stumped when trying to organize this list of > objects.  The objects in question are timesheets which i'd like to > sort by four attributes: > > class TimeSheet: >   department = string >   engagement = string >   date = datetime.date >

Trouble sorting a list of objects by attributes

2009-02-06 Thread rdmurray
Quoth Robocop : > Hello again, > I've found myself stumped when trying to organize this list of > objects. The objects in question are timesheets which i'd like to > sort by four attributes: > > class TimeSheet: > department = string > engagement

Trouble sorting a list of objects by attributes

2009-02-06 Thread Robocop
Hello again, I've found myself stumped when trying to organize this list of objects. The objects in question are timesheets which i'd like to sort by four attributes: class TimeSheet: department = string engagement = string date = datetime.date stare_hour = datetime.time M

Re: self-aware list of objects able to sense constituent member alterations?

2009-02-06 Thread greyw...@gmail.com
On Jan 28, 4:37 am, John O'Hagan wrote: > On Tue, 27 Jan 2009, Reckoner wrote: > > I'm not sure this is possible, but I would like to have > > a list of  objects > > > A=[a,b,c,d,...,z] > > > where,  in the midst of a lot of proc

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-30 Thread Robert Kern
On 2009-01-29 18:22, Reckoner wrote: I haven't looked at Enthought in awhile. I want to avoid having to installing the entire Enthought toolsuite, however. Would I have to do that for Traits? No, Traits can be installed by itself unless if you want its GUI capabilities. http://pypi.python

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-30 Thread Reckoner
27;m not sure this is possible, but I would like to have > > > > > a list of  objects > > > > > > A=[a,b,c,d,...,z] > > > > > > where,  in the midst of a lot of processing I might do something like, > > > > > > A[0].do_something_wh

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-29 Thread Reckoner
On Jan 28, 10:17 pm, Peter Wang wrote: > On Jan 27, 3:16 pm,Reckoner wrote: > > > > > I'm not sure this is possible, but I would like to have > > a list of  objects > > > A=[a,b,c,d,...,z] > > > where,  in the midst of a lot of

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread Peter Wang
On Jan 27, 3:16 pm, Reckoner wrote: > I'm not sure this is possible, but I would like to have > a list of  objects > > A=[a,b,c,d,...,z] > > where,  in the midst of a lot of processing I might do something like, > > A[0].do_something_which_changes_the_properties() &

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread Steven D'Aprano
On Wed, 28 Jan 2009 11:06:21 -0800, Reckoner wrote: > thanks for your reply. > > For the second case where > >> class TaintThing(object): >> parent = A >> def __init__(self, obj): >> self.__dict__['_proxy'] = obj >> def __getattr__(self, attr): >> return getattr(self.

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread Reckoner
On Jan 27, 9:46 pm, Steven D'Aprano wrote: > On Tue, 27 Jan 2009 13:16:36 -0800, Reckoner wrote: > > I'm not sure this is possible, but I would like to have a list of > > objects > > > A=[a,b,c,d,...,z] > > > where, in the midst of a lot of proc

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread koranthala
On Jan 28, 10:39 pm, Reckoner wrote: > On Jan 28, 9:16 am, koranthala wrote: > > > > > On Jan 28, 5:42 pm, koranthala wrote: > > > > On Jan 28, 2:16 am, Reckoner wrote: > > > > > I'm not sure this is possible, but I would like to have >

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread Reckoner
On Jan 28, 9:16 am, koranthala wrote: > On Jan 28, 5:42 pm, koranthala wrote: > > > > > On Jan 28, 2:16 am, Reckoner wrote: > > > > I'm not sure this is possible, but I would like to have > > > a list of objects > > > > A=[a,b,c,d,...,

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread Paul McGuire
On Jan 27, 3:16 pm, Reckoner wrote: > The trick is that I would like A to be mysteriously aware that > something about the  object 'a' has changed so that when I revisit A, > I will know that the other items in the list need to be refreshed to > reflect the changes in A as a result of changing 'a'

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread koranthala
On Jan 28, 5:42 pm, koranthala wrote: > On Jan 28, 2:16 am, Reckoner wrote: > > > > > I'm not sure this is possible, but I would like to have > > a list of  objects > > > A=[a,b,c,d,...,z] > > > where,  in the midst of a lot of

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread koranthala
On Jan 28, 2:16 am, Reckoner wrote: > I'm not sure this is possible, but I would like to have > a list of  objects > > A=[a,b,c,d,...,z] > > where,  in the midst of a lot of processing I might do something like, > > A[0].do_something_which_changes_the_properties() &

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread John O'Hagan
On Tue, 27 Jan 2009, Reckoner wrote: > I'm not sure this is possible, but I would like to have > a list of objects > > A=[a,b,c,d,...,z] > > where, in the midst of a lot of processing I might do something like, > > A[0].do_something_which_changes_the_properties() &

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-28 Thread Aaron Brady
On Jan 27, 3:16 pm, Reckoner wrote: > I'm not sure this is possible, but I would like to have > a list of  objects > > A=[a,b,c,d,...,z] > > where,  in the midst of a lot of processing I might do something like, > > A[0].do_something_which_changes_the_properties() &

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-27 Thread Steven D'Aprano
On Tue, 27 Jan 2009 13:16:36 -0800, Reckoner wrote: > I'm not sure this is possible, but I would like to have a list of > objects > > A=[a,b,c,d,...,z] > > where, in the midst of a lot of processing I might do something like, > > A[0].do_something_which_change

Re: self-aware list of objects able to sense constituent member alterations?

2009-01-27 Thread Chris Rebert
On Tue, Jan 27, 2009 at 1:16 PM, Reckoner wrote: > I'm not sure this is possible, but I would like to have > a list of objects > > A=[a,b,c,d,...,z] > > where, in the midst of a lot of processing I might do something like, > > A[0].do_something_which_changes_the_pro

self-aware list of objects able to sense constituent member alterations?

2009-01-27 Thread Reckoner
I'm not sure this is possible, but I would like to have a list of objects A=[a,b,c,d,...,z] where, in the midst of a lot of processing I might do something like, A[0].do_something_which_changes_the_properties() which alter the properties of the object 'a'. The trick is that

Re: "Find" in list of objects

2008-10-23 Thread bearophileHUGS
Andreas Müller: > is there a construct like > list.find (10, key='ID') Given the current Python a syntax like this is more probable: somelist.find(10, key=attrgetter('ID')) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: "Find" in list of objects

2008-10-23 Thread Gabriel Genellina
En Thu, 23 Oct 2008 05:23:51 -0200, Andreas Müller <[EMAIL PROTECTED]> escribió: (Python 2.2.3 if this is relevant :-) I have a list of objects with, lets say, the attributes "ID", "x" and "y". Now I want to find the index of list element with ID=10. O

Re: "Find" in list of objects

2008-10-23 Thread bearophileHUGS
Andreas Müller: > is there a construct like > list.find (10, key='ID') You can create yourself a little convenience function, or you can use something like the following. First some testing code: class C: def __init__(self, id): self.id = id def __repr__(self): return "<%s

"Find" in list of objects

2008-10-23 Thread Andreas Müller
Hi! (Python 2.2.3 if this is relevant :-) I have a list of objects with, lets say, the attributes "ID", "x" and "y". Now I want to find the index of list element with ID=10. Of course I can loop through the list manually, but is there a construct like list.fin

Re: Cast list of objects to list of strings

2008-06-03 Thread jay graves
On Jun 2, 8:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > Still nitpicking: using a generator expression in this case has no > advantage. The first thing that str.join does is to create a list out of > its argument (unless it is already a list or a tuple). In fact, a list > comprehension

Re: Cast list of objects to list of strings

2008-06-02 Thread Gabriel Genellina
En Mon, 02 Jun 2008 18:56:08 -0300, jay graves <[EMAIL PROTECTED]> escribió: On Jun 2, 4:02 pm, Larry Bates <[EMAIL PROTECTED]> wrote: I think what you want is: def write_err(*args): from sys import stderr stderr.write("\n".join([str(o) for o in args])) Slight nitpick. If you are

Re: Cast list of objects to list of strings

2008-06-02 Thread bukzor
On Jun 2, 2:56 pm, jay graves <[EMAIL PROTECTED]> wrote: > On Jun 2, 4:02 pm, Larry Bates <[EMAIL PROTECTED]> wrote: > > > I think what you want is: > > def write_err(*args): > > from sys import stderr > > stderr.write("\n".join([str(o) for o in args])) > > Slight nitpick. If you are usi

Re: Cast list of objects to list of strings

2008-06-02 Thread Gary Herron
t;.join([str(b) for b in objs])+"\n") Gary Herron What's the simplest way to fix this? In essence, I need to cast a list of objects to a list of strings. I'd like to do just "str(objs)" but that (obviously) doesn't quite do what I need. -- http://mail.py

Re: Cast list of objects to list of strings

2008-06-02 Thread jay graves
On Jun 2, 4:02 pm, Larry Bates <[EMAIL PROTECTED]> wrote: > I think what you want is: > def write_err(*args): > from sys import stderr > stderr.write("\n".join([str(o) for o in args])) Slight nitpick. If you are using version >= 2.4 you could use a generator expression instead of a list

Re: Cast list of objects to list of strings

2008-06-02 Thread Larry Bates
ot; ".join(objs)+"\n") but I lose the property that the function works on any object. What's the simplest way to fix this? In essence, I need to cast a list of objects to a list of strings. I'd like to do just "str(objs)" but that (obviously) doesn't quite do w

Cast list of objects to list of strings

2008-06-02 Thread bukzor
js)+"\n") but I lose the property that the function works on any object. What's the simplest way to fix this? In essence, I need to cast a list of objects to a list of strings. I'd like to do just "str(objs)" but that (obviously) doesn't quite do what I need. -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-09 Thread Jason
Thanks so much everyone! That works great, & (presumably) is way faster than the way I was doing it-- -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-09 Thread Peter Otten
Paddy wrote: > On Apr 9, 4:04 am, Jason <[EMAIL PROTECTED]> wrote: >> Hi folks-- >> >> Basically, I have a pressing need for a combination of 5.2 "Sorting a >> List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects >>

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread Paddy
On Apr 9, 4:04 am, Jason <[EMAIL PROTECTED]> wrote: > Hi folks-- > > Basically, I have a pressing need for a combination of 5.2 "Sorting a > List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects > by an Attribute of the Objects" from

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread Jason
On Apr 8, 8:26 pm, "David Harrison" <[EMAIL PROTECTED]> wrote: > On 09/04/2008, Jason <[EMAIL PROTECTED]> wrote: > > > Hi folks-- > > > Basically, I have a pressing need for a combination of 5.2 "Sorting a > > List of Strings Case-Inse

Re: Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread David Harrison
On 09/04/2008, Jason <[EMAIL PROTECTED]> wrote: > Hi folks-- > > Basically, I have a pressing need for a combination of 5.2 "Sorting a > List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects > by an Attribute of the Objects" from t

Sorting a List of Objects by an Attribute of the Objects Case-Insensitively

2008-04-08 Thread Jason
Hi folks-- Basically, I have a pressing need for a combination of 5.2 "Sorting a List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects by an Attribute of the Objects" from the Python Cookbook. My first guess isn't working: import operator def sort

Re: Rebuild list of objects with redundancies on objects' attribute

2008-01-10 Thread Peter Otten
Nico Grubert wrote: > Hi there > > I have a list of dummy objects which have the attributes 'location', > 'name', 'gender'. > Now I want to rebuild this list in order to avoid redundancies on > objects with the same 'location'. > > Example: > > #---

Rebuild list of objects with redundancies on objects' attribute

2008-01-10 Thread Nico Grubert
Hi there I have a list of dummy objects which have the attributes 'location', 'name', 'gender'. Now I want to rebuild this list in order to avoid redundancies on objects with the same 'location'. Example: #-- class Dummy: pas

Re: parsing string to a list of objects - help!

2007-11-05 Thread Donn Ingle
Wow Paul, you have given me much to chew. I'll start testing it in my slow way -- it looks so simple! Thank you. \d -- http://mail.python.org/mailman/listinfo/python-list

Re: parsing string to a list of objects - help!

2007-11-05 Thread Donn Ingle
Paul, I quickly slapped your example into a py file for a first-glance test and the first part works, but the second gives me the error below. Could I have an old version of pyparser? I will dig into it anyway, just being lazy :) code: from pyparsing import * frame = Literal("#") tween = Word("-"

Re: parsing string to a list of objects - help!

2007-11-05 Thread Donn Ingle
Wow Paul, you have given me much to chew. I'll start testing it in my slow way -- it looks so simple! Thank you. \d -- http://mail.python.org/mailman/listinfo/python-list

Re: parsing string to a list of objects - help!

2007-11-05 Thread Paul McGuire
On Nov 5, 3:00 am, Donn Ingle <[EMAIL PROTECTED]> wrote: > > I have glanced around at parsing and all the tech-speak confuses the heck > out of me. I am not too smart and would appreciate any help that steers > away from cold theory and simply hits at this problem. > Donn - Here is a pyparsing ve

parsing string to a list of objects - help!

2007-11-05 Thread Donn Ingle
Hi, I really hope someone can help me -- I'm stuck. I have written three versions of code over a week and still can't get past this problem, it's blocking my path to getting other code written. This might be a little hairy, but I'll try to keep it short. Situation: I want to pass a string to a

Re: List of objects X Database

2007-10-04 Thread Gerardo Herzig
his is not clear to me how can be implemented. I thought of creating >a class with the data structure, and creating a list of objects of >that class, each one containing one line of data from the "database". > >Any thoughts or suggestions? > >Thanks! >Eduardo > &g

Re: List of objects X Database

2007-10-04 Thread M.-A. Lemburg
. > > This is not clear to me how can be implemented. I thought of creating > a class with the data structure, and creating a list of objects of > that class, each one containing one line of data from the "database". > > Any thoughts or suggestions? Python 2.5 ships w

Re: List of objects X Database

2007-10-03 Thread Pierre Quentel
On 3 oct, 22:01, MindMaster32 <[EMAIL PROTECTED]> wrote: Hi, Maybe PyDbLite (http://quentel.pierre.free.fr/PyDbLite/index.html) is what you need : a single Python module, compatible with Python 2.3+, that lets you manipulate data in memory You can manage a database like this : import PyDbLite d

Re: List of objects X Database

2007-10-03 Thread Wildemar Wildenburger
Michael Bentley wrote: > > On Oct 3, 2007, at 1:01 PM, MindMaster32 wrote: > >> I am writing a script that has to read data from an ASCII file of >> about 50 Mb and do a lot of searches and calculations with that data. >> That would be a classic problem solved by the use of a database >> (SQLite

Re: List of objects X Database

2007-10-03 Thread Michael Bentley
and working with datafields. > > This is not clear to me how can be implemented. I thought of creating > a class with the data structure, and creating a list of objects of > that class, each one containing one line of data from the "database". > > Any thoughts or suggest

List of objects X Database

2007-10-03 Thread MindMaster32
creating a class with the data structure, and creating a list of objects of that class, each one containing one line of data from the "database". Any thoughts or suggestions? Thanks! Eduardo -- http://mail.python.org/mailman/listinfo/python-list

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-10 Thread Stefan Arentz
[EMAIL PROTECTED] (Alex Martelli) writes: > Stefan Arentz <[EMAIL PROTECTED]> wrote: > > > Miki <[EMAIL PROTECTED]> writes: > > > > > > steps.sort(key = lambda s: s.time) > > > This is why attrgetter in the operator module was invented. > > > from operator import attrgetter > > > ... > > > ste

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-09 Thread Alex Martelli
Stefan Arentz <[EMAIL PROTECTED]> wrote: > Miki <[EMAIL PROTECTED]> writes: > > > > steps.sort(key = lambda s: s.time) > > This is why attrgetter in the operator module was invented. > > from operator import attrgetter > > ... > > steps.sort(key=attrgettr("time")) > > Personally I prefer the a

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-09 Thread Stefan Arentz
Miki <[EMAIL PROTECTED]> writes: > > steps.sort(key = lambda s: s.time) > This is why attrgetter in the operator module was invented. > from operator import attrgetter > ... > steps.sort(key=attrgettr("time")) Personally I prefer the anonymous function over attrgettr :) S. -- http://mail.pyt

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-08 Thread Miki
> steps.sort(key = lambda s: s.time) This is why attrgetter in the operator module was invented. from operator import attrgetter ... steps.sort(key=attrgettr("time")) HTH, -- Miki <[EMAIL PROTECTED]> http://pythonwise.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-07 Thread Marc 'BlackJack' Rintsch
On Fri, 07 Sep 2007 06:57:35 -0700, cjt22 wrote: > I have a step class and store in a list step instances > A step instance contains variables: name, startTime etc and startTime > is stored as a string %H:%M:%S > > What I would like to do is to be able to sort this list of objects

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-07 Thread Stefan Arentz
tance contains variables: name, startTime etc and startTime > is stored as a string %H:%M:%S > > What I would like to do is to be able to sort this list of objects > based on the startTime object so that the first item in the list is > the object with the earliest Start time and last i

Re: Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-07 Thread Tim Golden
tance contains variables: name, startTime etc and startTime > is stored as a string %H:%M:%S > > What I would like to do is to be able to sort this list of objects > based on the startTime object so that the first item in the list is > the object with the earliest Start time and last i

Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

2007-09-07 Thread cjt22
as a string %H:%M:%S What I would like to do is to be able to sort this list of objects based on the startTime object so that the first item in the list is the object with the earliest Start time and last item is the object with the last Start time. I belive my key has to be = strpTime(step.sTime

Re: List of Objects

2007-04-20 Thread Hendrik van Rooyen
"Steve Holden" wrote: > Steven D'Aprano wrote: > [...] > > > > Forth method: create identical gazelles, then modify them: > > > > list_of_beasties = [Gazelle(defaults) for i in xrange(1000)] > > for i, beastie in enumerate(xrange(1000)): > > list_of_beasties[i] = modify(beastie) > > > N

Re: List of Objects

2007-04-19 Thread 7stud
On Apr 19, 9:18 pm, Paddy <[EMAIL PROTECTED]> wrote: > > # create a list of instances > gazelles= [ Gazelle() for x in range(5)] > Nice. I knew there had to be a way to use a list comprehension, but I couldn't figure it out. -- http://mail.python.org/mailman/listinfo/python-list

Re: List of Objects

2007-04-19 Thread Steve Holden
Steven D'Aprano wrote: [...] > > Forth method: create identical gazelles, then modify them: > > list_of_beasties = [Gazelle(defaults) for i in xrange(1000)] > for i, beastie in enumerate(xrange(1000)): > list_of_beasties[i] = modify(beastie) > Nope, 'sorry, that's Python a's well. Forth u'se

Re: List of Objects

2007-04-19 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Howdy, a (possibly) quick question for anyone willing to listen. > I have a question regarding lists and Classes; I have a class called It looks you don't really want what you're saying: you appear to want a list of INSTANCES of one class, *NOT* a list of CLASSES. E.

Re: List of Objects

2007-04-19 Thread [EMAIL PROTECTED]
These methods work. I didn't think I could create a list of objects like that, however, I stand corrected. Thanks for your quick (and helpful) responses! On Apr 19, 11:22 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Thu, 19 Apr 2007 19:58:35 -0700, datamonkey.ryan

Re: List of Objects

2007-04-19 Thread Mel Wilson
[EMAIL PROTECTED] wrote: > Howdy, a (possibly) quick question for anyone willing to listen. > I have a question regarding lists and Classes; I have a class called > "gazelle" with several attributes (color, position, etc.) and I need > to create a herd of them. I want to simulate motion of individu

Re: List of Objects

2007-04-19 Thread Steven D'Aprano
On Thu, 19 Apr 2007 19:58:35 -0700, datamonkey.ryan wrote: > Howdy, a (possibly) quick question for anyone willing to listen. > I have a question regarding lists and Classes; I have a class called > "gazelle" with several attributes (color, position, etc.) and I need > to create a herd of them. I

Re: List of Objects

2007-04-19 Thread DillonCo
On Thursday 19 April 2007, [EMAIL PROTECTED] wrote: > Howdy, a (possibly) quick question for anyone willing to listen. > I have a question regarding lists and Classes; I have a class called > "gazelle" with several attributes (color, position, etc.) and I need > to create a herd of them. I want to

Re: List of Objects

2007-04-19 Thread Paddy
On Apr 20, 3:58 am, [EMAIL PROTECTED] wrote: > Howdy, a (possibly) quick question for anyone willing to listen. > I have a question regarding lists and Classes; I have a class called > "gazelle" with several attributes (color, position, etc.) and I need > to create a herd of them. I want to simulat

Re: List of Objects

2007-04-19 Thread 7stud
[EMAIL PROTECTED] wrote: > > However, Python doesn't support pointers As I understand it, every name in Python is a pointer. class Gazelle(object): def __init__(self): self.x = 0 g_list =[] for x in range(10): g_list.append(Gazelle()) for g in g_list: g.x = 10 print g_list[

List of Objects

2007-04-19 Thread datamonkey . ryan
Howdy, a (possibly) quick question for anyone willing to listen. I have a question regarding lists and Classes; I have a class called "gazelle" with several attributes (color, position, etc.) and I need to create a herd of them. I want to simulate motion of individual gazelles, but I don't want to

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Azolex
Raymond Hettinger wrote: > Azolex: >> Let's push the diagnosis a bit further : the aversion to the keyword >> "lambda" has to do with the fact that it ignores the english word used >> by all non-geeks to convey the meaning, eg "given" > > Right. However, Guido has said that lambda is here to stay

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
[Noah] > I suggested the above because it wasn't obvious to me how one would > pass the arbitrary set of attributes to the lambda expression (and I > envisioned them being specified as strings in this case, since the set > of attributes will be coming from a web form). > > So what about the followi

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
Azolex: > Let's push the diagnosis a bit further : the aversion to the keyword > "lambda" has to do with the fact that it ignores the english word used > by all non-geeks to convey the meaning, eg "given" Right. However, Guido has said that lambda is here to stay, so it's time to get over it. R

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Azolex
Raymond Hettinger wrote: > > The cult of lambda avoidance has lost contact with reality. [...] > Lambda avoidance is rooted in two things, an aversion to the keyword > name [...] Let's push the diagnosis a bit further : the aversion to the keyword "lambda" has to do with the fact that it ignore

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread nghoffma
I'm sure my avoidance of lambdas as due as much to laziness as adherence to principle. This is a good opportunity to learn about them. I suggested the above because it wasn't obvious to me how one would pass the arbitrary set of attributes to the lambda expression (and I envisioned them being spe

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Scott David Daniels
Kent Johnson wrote: > Scott David Daniels wrote: >> Kent Johnson wrote: >>> In Python 2.5 you can do this with operator.attrgetter(): >>> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey')) >> >> Note: this is also available in Python 2.4 > > No, the ability to specify more th

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Raymond Hettinger
[George Young] >> For multiple keys the form is quite analogous: >> >> L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) [Noah] > If you are lambda-phobic (as I am) this should also work for an > arbitrary set of attributes: > > attrs = 'attr1 attr2 attr3'.split() > sortlist = [[getat

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Kent Johnson
Scott David Daniels wrote: > Kent Johnson wrote: >> In Python 2.5 you can do this with operator.attrgetter(): >> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey')) > > Note: this is also available in Python 2.4 No, the ability to specify more than one attribute name, making

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Scott David Daniels
Kent Johnson wrote: > In Python 2.5 you can do this with operator.attrgetter(): > L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey')) Note: this is also available in Python 2.4 --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Kent Johnson
gry@ll.mit.edu wrote: > For multiple keys the form is quite analogous: > >L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) > > I.e., just return a tuple with the keys in order from your lambda. > Such tuples sort nicely. In Python 2.5 you can do this with operator.attrgetter()

  1   2   >