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

mapping function to vars

I need to map a function to several variables. I'm trying to use map
and lambda to do this. Here's my attempt...

#!/usr/bin/env python
from random import *

[fee, fye, foe, fum] = map(lambda n: random(), range(4))

print fee
print fye
print foe
print fum

....I'm essentially trying to map a function that takes no parameters to
a group of variables. This works, but pychecker complains about the
'n' parameter. Is there a better way to do this? TIA

Jul 18 '05 #1
3 1405
ja****@hotmail.com wrote:
I need to map a function to several variables. I'm trying to use map
and lambda to do this. Here's my attempt...

#!/usr/bin/env python
from random import *

[fee, fye, foe, fum] = map(lambda n: random(), range(4))
from random import random
fee = random()
fye = random()
foe = random(),
fum = random()
print fee
print fye
print foe
print fum
...I'm essentially trying to map a function that takes no parameters to
a group of variables. This works, but pychecker complains about the
'n' parameter. Is there a better way to do this? TIA


Jul 18 '05 #2
ja****@hotmail.com wrote:
I need to map a function to several variables. I'm trying to use map
and lambda to do this. Here's my attempt...

#!/usr/bin/env python
from random import *

[fee, fye, foe, fum] = map(lambda n: random(), range(4))

print fee
print fye
print foe
print fum

...I'm essentially trying to map a function that takes no parameters to
a group of variables. This works, but pychecker complains about the
'n' parameter. Is there a better way to do this? TIA

import random
fee, fye, foe, fum = [random.random() for _ in range(4)]
fee, fye, foe, fum

(0.39415235335694276, 0.43533547827112462, 0.47106288849970501,
0.87920678036897715)

I don't know pychecker well enough, but I think it ignores variables
named _, so I think you could also just switch your n with _.
Personally, I find the list comprehension much more readable.

Steve
Jul 18 '05 #3

Steven Bethard wrote:
ja****@hotmail.com wrote:
I need to map a function to several variables. I'm trying to use map
and lambda to do this. Here's my attempt...

#!/usr/bin/env python
from random import *

[fee, fye, foe, fum] = map(lambda n: random(), range(4))

print fee
print fye
print foe
print fum

...I'm essentially trying to map a function that takes no parameters to a group of variables. This works, but pychecker complains about the 'n' parameter. Is there a better way to do this? TIA

>>> import random
>>> fee, fye, foe, fum = [random.random() for _ in range(4)]
>>> fee, fye, foe, fum

(0.39415235335694276, 0.43533547827112462, 0.47106288849970501,
0.87920678036897715)

I don't know pychecker well enough, but I think it ignores variables
named _, so I think you could also just switch your n with _.
Personally, I find the list comprehension much more readable.

Steve


Thanks! This is exactly what I was after :)

Jul 18 '05 #4

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

Similar topics

3
by: Zim | last post by:
Greetings, I have an all purpose variable dumper that uses Var_Dump to show me data structures. I have an application wide function that I simply call to dump a structure: vardump($var);
0
by: Uwe Mayer | last post by:
Hi, I've got a class that receives a function in the constructor and uses the __call__ method to execute the aforementioned function when the instance object is called: class foo(object):...
20
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' ...
7
by: vegetax | last post by:
I i need a decorator that adds a local variable in the function it decorates, probably related with nested scopes, for example: def dec(func): def wrapper(obj = None): if not obj : obj = Obj()...
6
by: naruto | last post by:
Hi all, I have the following being defined in a A.cxx file. // define in source file. Not exported to the outside world (this cannot be // moved to the header file ) #define CHANNEL_0 0...
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
0
by: Marty Scholes | last post by:
I may have found a bug. I have a table: CREATE TABLE onlpcd_stat ( sel BIGSERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES onlpcd_user ON DELETE CASCADE, vars TEXT, /*...
0
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no*...
0
by: Fredrik Lundh | last post by:
Andreas Tawn wrote: the for-in loop does ordinary assignments in the current scope: http://docs.python.org/ref/for.html "Each item in turn is assigned to the target list using the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.