473,406 Members | 2,404 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,406 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 1410
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.