473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Making things more functional in Python

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/
Jul 18 '05 #1
6 1239
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
Jul 18 '05 #2
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/
Jul 18 '05 #3
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
Jul 18 '05 #4
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/
Jul 18 '05 #5
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
Jul 18 '05 #6
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
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
12
by: Ryan Paul | last post by:
I've spent a lot of time using python, and personally, I feel like it is vastly superior when compared to languages like java, and c++, but there are still a few things that detract from its...
8
by: beza1e1 | last post by:
I see myself shifting more and more over to the functional kind of coding. Could be related to the Haskell, we had to learn in CS. Now i was wondering, how other people use Python? With...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
17
by: ToddLMorgan | last post by:
I'm just starting out with python, after having a long history with Java. I was wondering if there were any resources or tips from anyone out there in Python-land that can help me make the...
15
by: Lorenzo Stella | last post by:
Hi all, I haven't experienced functional programming very much, but now I'm trying to learn Haskell and I've learned that: 1) in functional programming LISTS are fundmental; 2) any "cycle" in FP...
16
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
10
by: James Fassett | last post by:
Hi all, Had a simple problem that turned into an interesting solution and I thought I would share it here. I had a list of tuples that I needed to get the first value from and generate a...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.