Array construction from object members

M

MKoool

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.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!
 
G

Gerard Flanagan

MKoool said:
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.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
 
P

Paul McGuire

MKoool said:
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,
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 )
 
S

Steven D'Aprano

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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top