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. 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 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.
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
"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. 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.
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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.
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
| |