By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

Array construction from object members

P: n/a
Hi everyone,

I am doing several operations on lists and I am wondering if python has
anything built in to get every member of several objects that are in an
array, for example, if i have a class like the following:

class myClass:
a = 0.0

And lets say I populate the "a" element in an array of objects of
myClass. If I want to retrieve all items in this and perhaps give it
to a mean function, I would need to make a loop now:

mySimpleArray = []
for i in range(0,len(myArray)):
mySimpleArray.append(myArray[i].a)

There must be some more efficient way to do this, can someone point me
to the right direction so that I can review some documentation and get
things a little more efficient?

thanks!

Dec 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
MKoool wrote:
Hi everyone,

I am doing several operations on lists and I am wondering if python has
anything built in to get every member of several objects that are in an
array, for example, if i have a class like the following:

class myClass:
a = 0.0

And lets say I populate the "a" element in an array of objects of
myClass. If I want to retrieve all items in this and perhaps give it
to a mean function, I would need to make a loop now:

mySimpleArray = []
for i in range(0,len(myArray)):
mySimpleArray.append(myArray[i].a)

There must be some more efficient way to do this, can someone point me
to the right direction so that I can review some documentation and get
things a little more efficient?

thanks!


not sure if this is what you want to do, but does this help:

class myclass(object):
def __init__(self, a):
self.a = a

mylist = [ myclass('one'), myclass('two'), myclass('three'),
myclass('four') ]

alist = [ A.a for A in mylist ] #this is called a 'list comprehension'

print alist

output: ['one', 'two', 'three', 'four']

Gerard

Dec 31 '05 #2

P: n/a
"MKoool" <mo**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everyone,

I am doing several operations on lists and I am wondering if python has
anything built in to get every member of several objects that are in an
array, <-snip->

Here's some sample code to show you how list comprehensions and generator
expressions do what you want.

-- Paul
class A(object):
def __init__(self,val):
self.a = val

# define _repr_ to make it easy to print list of A's
def __repr__(self):
return "A(%s)" % str(self.a)

Alist = [ A(i*1.5) for i in range(5) ]

print Alist

# create new list of .a attributes, print, and sum
Alist_avals = [ x.a for x in Alist ]
print Alist_avals
print sum(Alist_avals)

# if original list is much longer...
Alist = [ A(i*1.5) for i in range(500000) ]

# ... creating list comprehension will take a while...
print sum( [ x.a for x in Alist ] )

# ... instead use generator expression - avoids creation of new list
print sum( x.a for x in Alist )
Dec 31 '05 #3

P: n/a
On Sat, 31 Dec 2005 09:01:44 -0800, MKoool wrote:
Hi everyone,

I am doing several operations on lists and I am wondering if python has
anything built in to get every member of several objects that are in an
array, for example, if i have a class like the following:

class myClass:
a = 0.0
Did you mean for a to be a class attribute? You possibly want something
like this:

class myClass:
def __init__(self, value=0.0):
self.a = value # "a" for attribute

Then you can create new instances:

fred = myClass(2.7)
wilma = myClass() # just use the default
betty = myClass(1.3)
barney = myClass(0.9)

And lets say I populate the "a" element in an array of objects of
myClass. If I want to retrieve all items in this and perhaps give it
to a mean function, I would need to make a loop now:

mySimpleArray = []
for i in range(0,len(myArray)):
mySimpleArray.append(myArray[i].a)

There must be some more efficient way to do this, can someone point me
to the right direction so that I can review some documentation and get
things a little more efficient?


myArray = [fred, wilma, betty, barney]

mySimpleArray = []
for person in myArray:
mySimpleArray.append(person.a)
Or try this:

mySimpleArray = [person.a for person in myArray]

Or this:

def mean_value(*people):
"""Takes a list of people and returns the mean of their attributes"""
total = 0.0
for person in people:
total += person.a
return total/len(people)

mean_value(fred, betty, barney, wilma)

That last version is not recommended, because it requires a separate
function for everything you want to calculate. For instance, if you add a
second attribute "height" to myClass, and want to work out the mean
height, you would need a second function to do it.

--
Steven.

Jan 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.