Connecting Tech Pros Worldwide Forums | Help | Site Map

mapping function to vars

jahurt@hotmail.com
Guest
 
Posts: n/a
#1: Jul 18 '05
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


Brian Quinlan
Guest
 
Posts: n/a
#2: Jul 18 '05

re: mapping function to vars


jahurt@hotmail.com wrote:[color=blue]
> 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))[/color]

from random import random
fee = random()
fye = random()
foe = random(),
fum = random()
[color=blue]
> print fee
> print fye
> print foe
> print fum[/color]

[color=blue]
> ...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[/color]

Steven Bethard
Guest
 
Posts: n/a
#3: Jul 18 '05

re: mapping function to vars


jahurt@hotmail.com wrote:[color=blue]
> 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
>[/color]
[color=blue][color=green][color=darkred]
>>> import random
>>> fee, fye, foe, fum = [random.random() for _ in range(4)]
>>> fee, fye, foe, fum[/color][/color][/color]
(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
jahurt@hotmail.com
Guest
 
Posts: n/a
#4: Jul 18 '05

re: mapping function to vars



Steven Bethard wrote:[color=blue]
> jahurt@hotmail.com wrote:[color=green]
> > I need to map a function to several variables. I'm trying to use[/color][/color]
map[color=blue][color=green]
> > 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[/color][/color]
parameters to[color=blue][color=green]
> > a group of variables. This works, but pychecker complains about[/color][/color]
the[color=blue][color=green]
> > 'n' parameter. Is there a better way to do this? TIA
> >[/color]
>[color=green][color=darkred]
> >>> import random
> >>> fee, fye, foe, fum = [random.random() for _ in range(4)]
> >>> fee, fye, foe, fum[/color][/color]
> (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[/color]

Thanks! This is exactly what I was after :)

Closed Thread