map-function

Can I use index information inside the map function?

雨燕双飞 提交于 2019-11-30 04:48:11
Let's assume there is a list a = [1, 3, 5, 6, 8] . I want to apply some transformation on that list and I want to avoid doing it sequentially, so something like map(someTransformationFunction, a) would normally do the trick, but what if the transformation needs to have knowledge of the index of each object? For example let's say that each element must be multiplied by its position. So the list should be transformed to a = [0, 3, 10, 18, 32] . Is there a way to do that? Use the enumerate() function to add indices: map(function, enumerate(a)) Your function will be passed a tuple , with (index,

Why map(print, a_list) doesn't work?

大兔子大兔子 提交于 2019-11-29 09:12:41
For a normal function, map works well: def increment(n): return n+1 l = [1, 2, 3, 4, 5] l = map(increment, l) print l >>> [2, 3, 4, 5, 6] However, if it's print being put inside the map function: l = [1, 2, 3, 4, 5] l = map(print, l) print l python will complain: l = map(print, l) ^ SyntaxError: invalid syntax What makes print special? Doesn't print(x) also a valid function call? The above code are tested under python 2.7. In Python 2.x, print is a statement, not a function. If you try this in Python 3.x it will work. In Python 2.x, you can say print(x) and it is not a syntax error, but it isn

multiprocessing pool.map call functions in certain order

假装没事ソ 提交于 2019-11-29 06:07:17
问题 How can I make multiprocessing.pool.map distribute processes in numerical order? More Info: I have a program which processes a few thousand data files, making a plot of each one. I'm using a multiprocessing.pool.map to distribute each file to a processor and it works great. Sometimes this takes a long time, and it would be nice to look at the output images as the program is running. This would be a lot easier if the map process distributed the snapshots in order; instead, for the particular

Using multiple map functions vs. a block statement in a map in a java stream

北慕城南 提交于 2019-11-29 05:55:34
Say I have the following code data.stream() .map(x -> { Object a = maybeReturnsNull(x); return a == null ? defaultValue : a; }) I have some function that might be returning null , and I'm applying it to an element of the stream. I then want to make sure that any null results get changed to some default value instead. Is there any significant difference between using two maps as in the following example, as compared to using the previous example that defines a helper variable a and uses a code block in the lambda expression? data.stream() .map(x -> maybeReturnsNull(x)) .map(x -> x == null ?

Pass multiple parameters to concurrent.futures.Executor.map?

岁酱吖の 提交于 2019-11-28 20:08:29
The concurrent.futures.Executor.map takes a variable number of iterables from which the function given is called. How should I call it if I have a generator that produces tuples that are normally unpacked in place? The following doesn't work because each of the generated tuples is given as a different argument to map: args = ((a, b) for (a, b) in c) for result in executor.map(f, *args): pass Without the generator, the desired arguments to map might look like this: executor.map( f, (i[0] for i in args), (i[1] for i in args), ..., (i[N] for i in args), ) vz0 You need to remove the * on the map

Using Array.map with new Array constructor

ぃ、小莉子 提交于 2019-11-28 11:44:50
I was trying to use new Array() constructor with map in order to create a one-line code that creates a list of elements. Something like this : let arr = new Array(12).map( (el, i) => { console.log('This is never called'); return i + 1; }); Reading docs , the behaviour makes sense. Basically docs say that callback of map will be executed even for declared undefined values in array, but not for example when creating empty Arrays like the code before. So this should work : var arr = new Array(12); for(let i = 0; i < arr.length ; i++){ arr[i] = undefined; } let list = arr.map( (e, i) => { console

Python 3 vs Python 2 map behavior

假装没事ソ 提交于 2019-11-28 07:32:25
In Python 2, a common (old, legacy) idiom is to use map to join iterators of uneven length using the form map(None,iter,iter,...) like so: >>> map(None,xrange(5),xrange(10,12)) [(0, 10), (1, 11), (2, None), (3, None), (4, None)] In Python 2, it is extended so that the longest iterator is the length of the returned list and if one is shorter than the other it is padded with None . In Python 3, this is different. First, you cannot use None as an argument for the callable in position 1: >>> list(map(None, range(5),range(10,12))) Traceback (most recent call last): File "<stdin>", line 1, in

Why map(print, a_list) doesn't work?

▼魔方 西西 提交于 2019-11-28 02:38:33
问题 For a normal function, map works well: def increment(n): return n+1 l = [1, 2, 3, 4, 5] l = map(increment, l) print l >>> [2, 3, 4, 5, 6] However, if it's print being put inside the map function: l = [1, 2, 3, 4, 5] l = map(print, l) print l python will complain: l = map(print, l) ^ SyntaxError: invalid syntax What makes print special? Doesn't print(x) also a valid function call? The above code are tested under python 2.7. 回答1: In Python 2.x, print is a statement, not a function. If you try

Java: is there a map function?

房东的猫 提交于 2019-11-27 17:38:48
I need a map function. Is there something like this in Java already? (For those who wonder: I of course know how to implement this trivial function myself...) There is no notion of a function in the JDK as of java 6. Guava has a Function interface though and the Collections2.transform(Collection<E>, Function<E,E2>) method provides the functionality you require. Example: // example, converts a collection of integers to their // hexadecimal string representations final Collection<Integer> input = Arrays.asList(10, 20, 30, 40, 50); final Collection<String> output = Collections2.transform(input,

What is the difference between .map, .every, and .forEach?

依然范特西╮ 提交于 2019-11-27 16:44:15
I've always wondered what the difference between them were. They all seem to do the same thing... The difference is in the return values. .map() returns a new Array of objects created by taking some action on the original item. .every() returns a boolean - true if every element in this array satisfies the provided testing function. An important difference with .every() is that the test function may not always be called for every element in the array. Once the testing function returns false for any element, no more array elements are iterated. Therefore, the testing function should usually have