Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making things more functional in Python

Reply
Thread Tools

Making things more functional in Python

 
 
gf gf
Guest
Posts: n/a
 
      03-04-2005
Is there a better, more FP style, more Pythonic way to
write this:

def compute_vectors(samples, dset):
vectors = {}
for d in dset:
vectors[d] = [sample.get_val(d) for sample in
samples]
return vectors

Namely, I'd like to get rid of the initilization
(vectors = {}) and also the loop Yet, I'd hate to put
an assignment into Python's FP list comprehensions.

Ideally, I'd like something like this:
vectors.dict_add({d:result}) for [sample.get_val(d)
for sample in samples for d in dset].

Is there anything like that? Am I missing the
picture?

Thanks.

PS If possible, please cc me on all responses, thanks.




__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/
 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      03-04-2005
gf gf wrote:
> Is there a better, more FP style, more Pythonic way to
> write this:
>
> def compute_vectors(samples, dset):
> vectors = {}
> for d in dset:
> vectors[d] = [sample.get_val(d) for sample in
> samples]
> return vectors
>
> Namely, I'd like to get rid of the initilization
> (vectors = {}) and also the loop


Generate the whole dictionary on the fly with a Python 2.4 generator
expression:

dict((d, [sample.get_val(d) for sample in samples]) for d in dset)

Whether this is "better" or not I think mainly hinges on which
one you ahve an easier time understanding later. Personally I would
prefer this version, but it's easy to get carried away trying to
functionalize things to the point that a procedural version is much
easier to understand.

> Yet, I'd hate to put an assignment into Python's FP list
> comprehensions.


Indeed it's not possible to have an assignment in a list comprehension.
(Unless it's a side-effect due to a function called by the list
comprehension.)

> Ideally, I'd like something like this:
> vectors.dict_add({d:result}) for [sample.get_val(d)
> for sample in samples for d in dset].


You can't use the name "vectors" without first initializing it
somehow!
--
Michael Hoffman
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-04-2005
gf gf wrote:
> Is there a better, more FP style, more Pythonic way to
> write this:
>
> def compute_vectors(samples, dset):
> vectors = {}
> for d in dset:
> vectors[d] = [sample.get_val(d) for sample in
> samples]
> return vectors
>
> Namely, I'd like to get rid of the initilization
> (vectors = {}) and also the loop Yet, I'd hate to put
> an assignment into Python's FP list comprehensions.
>
> Ideally, I'd like something like this:
> vectors.dict_add({d:result}) for [sample.get_val(d)
> for sample in samples for d in dset].
>
> Is there anything like that? Am I missing the
> picture?
>
> Thanks.
>
> PS If possible, please cc me on all responses, thanks.
>

The logical thing to use would be

return dict([(d, sample.getval(d)) for d in dset for sample in samples])

which (I think) should work from 2.2 onwards.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      03-04-2005
Steve Holden wrote:

> return dict([(d, sample.getval(d)) for d in dset for sample in samples])


That won't do what the original code does. This sets dict[d] to
samples[-1].getval(d) instead of [sample.getval(d) for sample in samples].
--
Michael Hoffman
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-04-2005
Michael Hoffman wrote:
> Steve Holden wrote:
>
>> return dict([(d, sample.getval(d)) for d in dset for sample in samples])

>
>
> That won't do what the original code does. This sets dict[d] to
> samples[-1].getval(d) instead of [sample.getval(d) for sample in samples].


My bad, I didn;t look closely enbough to see the need for the nested
comprehensions.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
 
Reply With Quote
 
Dave Benjamin
Guest
Posts: n/a
 
      03-05-2005
On Fri, 2005-03-04 at 08:36 -0800, gf gf wrote:
> Is there a better, more FP style, more Pythonic way to
> write this:
>
> def compute_vectors(samples, dset):
> vectors = {}
> for d in dset:
> vectors[d] = [sample.get_val(d) for sample in
> samples]
> return vectors


You could use reduce:

def compute_vectors(samples, dset):
def add_entry(vectors, d):
vectors[d] = [sample.get_val(d) for sample in samples]
return vectors
return reduce(add_entry, dset, {})

Dave


 
Reply With Quote
 
Dave Benjamin
Guest
Posts: n/a
 
      03-05-2005
On Sat, 2005-03-05 at 00:00 -0700, Dave Benjamin wrote:
> On Fri, 2005-03-04 at 08:36 -0800, gf gf wrote:
> > Is there a better, more FP style, more Pythonic way to
> > write this:
> >
> > def compute_vectors(samples, dset):
> > vectors = {}
> > for d in dset:
> > vectors[d] = [sample.get_val(d) for sample in
> > samples]
> > return vectors

>
> You could use reduce:
>
> def compute_vectors(samples, dset):
> def add_entry(vectors, d):
> vectors[d] = [sample.get_val(d) for sample in samples]
> return vectors
> return reduce(add_entry, dset, {})


This could be further generalized:

def compute(xs, ys, f):
def add_entry(result, y):
result[y] = [f(x, y) for x in xs]
return result
return reduce(add_entry, ys, {})

Now, compute_vectors is just:

compute(samples, dset, lambda x, y: x.get_val(y))

You could even abstract the method call:

def method(name):
def _method(obj, *args, **kwds):
return getattr(obj, name)(*args, **kwds)
return _method

compute(samples, dset, method('get_val'))

Dave


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functional way to compare things inside a list thorsopia@lavabit.com Python 15 09-23-2012 04:09 AM
Re: Functional way to compare things inside a list Chris Rebert Python 0 09-21-2012 08:33 AM
Re: Functional way to compare things inside a list Chris Angelico Python 0 09-21-2012 08:31 AM
Re: Functional way to compare things inside a list Chris Rebert Python 0 09-21-2012 08:28 AM
Re: Functional way to compare things inside a list Chris Angelico Python 0 09-21-2012 08:23 AM



Advertisments