Re: Vectorization and Numeric (Newbie)
- From: johnzenger@xxxxxxxxx
- Date: 28 Feb 2006 08:21:12 -0800
"map" takes a function and a list, applies the function to each item in
a list, and returns the result list. For example:
def f(x): return x + 4
[8, 12, 19, 20, 27, 46]numbers = [4, 8, 15, 16, 23, 42]
map(f, numbers)
So, rather that ask if there is a different way to write f, I'd just
use f in a different way.
Another way to accomplish the same result is with a list comprehension:
[8, 12, 19, 20, 27, 46][f(x) for x in numbers]
As you can see, if you wanted to "calculate all the values from 1 to
n," you could also use these techniques instead of a loop.
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]n = 10
[f(x) for x in xrange(1, n+1)]
.
- References:
- Vectorization and Numeric (Newbie)
- From: Ronny Mandal
- Vectorization and Numeric (Newbie)
- Prev by Date: Re: comple list slices
- Next by Date: Re: comple list slices
- Previous by thread: Re: Vectorization and Numeric (Newbie)
- Next by thread: compiled program input
- Index(es):
Relevant Pages
|