473,385 Members | 1,707 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,385 software developers and data experts.

Map with an extra parameter

Hi,
I'm not really sure how to explain this so maybe some example code is
best. This code makes a list of objects by taking a list of ints and
combining them with a constant:

class foo:
def __init__(self):
self.a = 0
self.b = 0

def func(a,b):
f = new foo()
f.a = a
f.b = b
return f

constants = [1]*6
vars = [1,2,3,4,5,6]
objects = map(func,vars,constants)

In the real world I need to do this as quick as possible (without
resorting to c) and there are several constants. (The constant is only
constant to each list so I can't make it a default argument to func.)
My question is, can anyone think of a way to do this efficiently
without having to use the `dummy` list of constants and would it be
quicker?

M.

Sep 9 '06 #1
8 1659
This may be what you need:

class foo:
def __init__(self, a, b):
self.a = a
self.b = b

vars = [1,2,3,4,5,6]
objects = [foo(a, 1) for a in vars]
Note that in Python the new is expressed wit the () at the end:
f = new foo()
Bye,
bearophile

Sep 9 '06 #2
be************@lycos.com wrote:
This may be what you need:

class foo:
def __init__(self, a, b):
self.a = a
self.b = b

vars = [1,2,3,4,5,6]
objects = [foo(a, 1) for a in vars]
Note that in Python the new is expressed wit the () at the end:
f = new foo()

Bye,
bearophile
(Not sure if this group likes top or bottom posts, sorry)
Thanks for the reply,
In the interests of speed my thinking was that using map would move the
loop out of Python and into C, is that the case when using list
comprehension? I'd always thought it was just syntatic short hand for
a Python loop.

M.

Sep 9 '06 #3
ml1n wrote:
In the interests of speed my thinking was that using map would move the
loop out of Python and into C, is that the case when using list
comprehension? I'd always thought it was just syntatic short hand for
a Python loop.
In Python the faster things are often the most simple.
You can time your code to see what's faster.
And even better you may try the simpler solution, and if the program
results too much slow with a profiling you can find the spots needing
improvements.

Bye,
bearophile

Sep 9 '06 #4
"ml1n" <em***@brizzle.comwrites:
In the interests of speed my thinking was that using map would move the
loop out of Python and into C, is that the case when using list
comprehension? I'd always thought it was just syntatic short hand for
a Python loop.
Best to run benchmarks, but I don't expect a huge difference between
any of them.
Sep 9 '06 #5
be************@lycos.com wrote:
ml1n wrote:
In the interests of speed my thinking was that using map would move the
loop out of Python and into C, is that the case when using list
comprehension? I'd always thought it was just syntatic short hand for
a Python loop.

In Python the faster things are often the most simple.
You can time your code to see what's faster.
And even better you may try the simpler solution, and if the program
results too much slow with a profiling you can find the spots needing
improvements.

Bye,
bearophile
Looks like someone already did:
http://mail.python.org/pipermail/pyt...ry/259870.html

Thanks for the shove in the right direction.

M.

Sep 9 '06 #6
ml1n:
Looks like someone already did:
http://mail.python.org/pipermail/pyt...ry/259870.html
Don't belive too much in such general timings. Time your specific code
when you think you need a true answer. Timing Python code is very easy
and fast, and sometimes results are surprising.

Bye,
bearophile

Sep 9 '06 #7
ml1n wrote:
be************@lycos.com wrote:
This may be what you need:

class foo:
def __init__(self, a, b):
self.a = a
self.b = b

vars = [1,2,3,4,5,6]
objects = [foo(a, 1) for a in vars]
Note that in Python the new is expressed wit the () at the end:
f = new foo()
Bye,
bearophile

(Not sure if this group likes top or bottom posts, sorry)
Thanks for the reply,
In the interests of speed my thinking was that using map would move the
loop out of Python and into C, is that the case when using list
comprehension? I'd always thought it was just syntatic short hand for
a Python loop.

M.
Your thinking is correct. :-) Check out
http://groups.google.ca/group/comp.l...56cf1a052b9c5d
wherein Fredrik Lundh shows the difference by bytecode disassembly.

Peace,
~Simon

P.S. Bottom posts.

Sep 9 '06 #8
ml1n wrote:
I'm not really sure how to explain this so maybe some example code is
best. This code makes a list of objects by taking a list of ints and
combining them with a constant:

class foo:
def __init__(self):
self.a = 0
self.b = 0

def func(a,b):
f = new foo()
f.a = a
f.b = b
return f

constants = [1]*6
vars = [1,2,3,4,5,6]
objects = map(func,vars,constants)

In the real world I need to do this as quick as possible (without
resorting to c) and there are several constants. (The constant is only
constant to each list so I can't make it a default argument to func.)
My question is, can anyone think of a way to do this efficiently
without having to use the `dummy` list of constants and would it be
quicker?
Here are two more ways to achieve what you want, but you have to time them
youself.
>>from itertools import starmap, izip, repeat
class Foo:
.... def __init__(self, a, b):
.... self.a = a
.... self.b = b
.... def __repr__(self):
.... return "Foo(a=%r, b=%r)" % (self.a, self.b)
....
>>constants = repeat(1)
vars = [1, 2, 3]
list(starmap(Foo, izip(vars, constants)))
[Foo(a=1, b=1), Foo(a=2, b=1), Foo(a=3, b=1)]

This requires Python 2.5:
>>from functools import partial
map(partial(Foo, b=1), vars)
[Foo(a=1, b=1), Foo(a=2, b=1), Foo(a=3, b=1)]
Peter
Sep 9 '06 #9

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

Similar topics

2
by: Dokter Z | last post by:
We are running into problems on our development environment with extra parameters being added on to a stored procedure call. Instead of the expected stored procedure call, the trace shows...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
9
by: Neil Ginsberg | last post by:
I have a strange situation using Access to automate a Word mail merge. Using Access 2000 and Word 2000, the code opens Word, opens the document in Word, sets a table in the calling Access...
2
by: saleek | last post by:
I was wondering if there is a way I can add an extra header to a datagrid? I found this solution on the internet - but it seems quite old and didn't work for me. ...
0
by: Fred Dag | last post by:
Hi, I have a FormView that uses a ObjectDataSource. It retreives data alright but when updating it gives a ObjectDataSource 'ObjectDataSourceCatalogueItem' could not find a non-generic method...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
2
by: binder | last post by:
How do I eliminate an extra backslash that is appearing after LoadXML call? This issue is causing an error with Process.Start. I have a string stored in sql: c:\program files\internet...
0
by: jobs | last post by:
I have a gridview that I use to both select a record to be edited in a formview and to also delete records with. Because the selectcommand is grouping rows, my deletes stored procedure requires a...
5
by: Dave | last post by:
I am using some recipes to do some calculation. But there are situation that I need to give extra parameters to the existing function. For example, the recipe has a function called: foo (int x,...
25
by: DanicaDear | last post by:
I am trying my hand at my first ever Access report. It is based on a two parameter query (start date, end date). When I use the report wizard and click "view report" the report prompts me for the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.