Questions tagged [map-function]

Higher-order function "map" transforms a collection by applying a given function to each element in it. USE [maps] for geography, [map] or [dictionary] for data types.

A map function is a higher-order function that applies a function to each element in a list, building a list of the results. Examples include map in Python, Scheme, Haskell and Scala, Stream.map in Java 8, array_map in PHP, and std::transform in C++.

A generalized map works with some general type of a collection, applying a function to each element in the collection and building the collection (of the same general type) of the results. An example is fmap in Haskell, etc.

For questions about geography, use the tag instead.

For questions about data types, use the tag instead, which is a synonym tag for .

829 questions
20
votes
2 answers

multiprocessing pool.map call functions in certain order

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…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
20
votes
4 answers

Python 3 vs Python 2 map behavior

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…
the wolf
  • 34,510
  • 13
  • 53
  • 71
19
votes
2 answers

how to use strip in map function

I'd like to use map to get list of strings: value = '1, 2, 3' my_list = list(map(strip, value.split(','))) but got: NameError: name 'strip' is not defined expected result: my_list=['1','2','3']
Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44
18
votes
4 answers

Python: map in place

I was wondering if there is a way to run map on something. The way map works is it takes an iterable and applies a function to each item in that iterable producing a list. Is there a way to have map modify the iterable object itself?
johannix
  • 29,188
  • 15
  • 39
  • 42
18
votes
6 answers

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

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…
clwen
  • 20,004
  • 31
  • 77
  • 94
17
votes
8 answers

Python: Anyway to use map to get first element of a tuple

I have a tuple of tuples and I want to put the first value in each of the tuples into a set. I thought using map() would be a good way of doing this the only thing is I can't find an easy way to access the first element in the tuple. So for example…
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
16
votes
3 answers

Map modify array of objects in Swift 2.2 (3.0)

I want to be able to modify my array of objects using map in Swift of the fly, without looping through each element. Before here were able to do something like this (Described in more details here: gnomes = gnomes.map { (var gnome: Gnome) -> Gnome…
kernelpanic
  • 2,876
  • 3
  • 34
  • 58
15
votes
5 answers

Mapping a nested list with List Comprehension in Python?

I have the following code which I use to map a nested list in Python to produce a list with the same structure. >>> nested_list = [['Hello', 'World'], ['Goodbye', 'World']] >>> [map(str.upper, x) for x in nested_list] [['HELLO', 'WORLD'],…
kjfletch
  • 5,394
  • 3
  • 32
  • 38
14
votes
2 answers

Map, Filter, Foldr in DrRacket/Scheme

Programming language: Scheme/DrRacket We're currently going over map, filter, and foldr in my comp sci class. I understand that all three can be used to create abstract functions, but I am honestly a little confused about the difference between the…
Lukas Pleva
  • 381
  • 2
  • 5
  • 17
14
votes
1 answer

Is performing a mapping operation without using returned value an antipattern?

Lets say I have a list and I want to add some values into it using a mapping function: const arr = [1, 2, 3, 4, 5]; const anotherArr = []; I do this using a functional approach: arr.map((item) => anotherArr.push(item)); Is this an antipattern /…
14
votes
6 answers

How to implement map using reduce in Clojure

In the book Clojure for the Brave and True at the end of the section covering reduce there's a challenge: If you want an exercise that will really blow your hair back, try implementing map using reduce It turns out that this was a lot harder (at…
mluisbrown
  • 14,448
  • 7
  • 58
  • 86
13
votes
3 answers

Creating an associative array in JavaScript using the map function

I've an array of objects with the following format [{'list': 'one', 'item': 1}, {'list': 'one', 'item': 2}, {'list': 'one', 'item': 3}, {'list': 'two', 'item': 1}, {'list': 'two', 'item': 2}] And I want to transform it like this [{'one': [1, 2,…
12
votes
4 answers

Simple idiom to break an n-long list into k-long chunks, when n % k > 0?

In Python, it is easy to break an n-long list into k-size chunks if n is a multiple of k (IOW, n % k == 0). Here's my favorite approach (straight from the docs): >>> k = 3 >>> n = 5 * k >>> x = range(k * 5) >>> zip(*[iter(x)] * k) [(0, 1, 2), (3,…
kjo
  • 33,683
  • 52
  • 148
  • 265
12
votes
2 answers

Python 3 map dictionary update method to a list of other dictionaries

In Python 2 I can do the following: >> d = {'a':1} >> extras = [{'b':2}, {'c':4}] >> map(d.update, extras) >> d['c'] >> 4 In Python 3 in get a KeyError: >> d = {'a':1} >> extras = [{'b':2}, {'c':4}] >> map(d.update, extras) >> d['c'] >> KeyError:…
mattgathu
  • 1,129
  • 1
  • 19
  • 28
11
votes
6 answers

Python how to reduce multiple lists?

I am able to use map and sum to achieve this functionality, but how to use reduce? There are 2 lists: a, b, they have same number of values. I want to calculate a[0]*b[0]+a[1]*b[1]+...+a[n]*b[n] The working version I wrote using map is value = …
zs2020
  • 53,766
  • 29
  • 154
  • 219
1 2
3
55 56