Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: calling method in the map function

map() and list comprehension are roughly equivalent:

map(function, list1)
[function(i) for i in list1]

What if the function we want to use is a method?

[i.function() for i in list1]
map(.function, list1) # error!
map(run_method(function), list1) # error!

How could I perform this kind of manipulation with map?

like image 965
Remi.b Avatar asked Feb 16 '14 16:02

Remi.b


People also ask

How do I iterate over a map object in Python?

You need to look up the help for dict(). It's all there -- 'for k in mp' iterates over keys, 'for v in mp. values()' iterates over values, 'for k,v in mp. items()' iterates over key, value pairs.

How do you read a map object in Python?

map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.) Parameters : fun : It is a function to which map passes each element of given iterable. iter : It is a iterable which is to be mapped.

How do you use the map function?

To use the map() function, attach it to an array you want to iterate over. The map() function expects a callback as the argument and executes it once for each element in the array. From the callback parameters, you can access the current element, the current index, and the array itself.


2 Answers

You'd use operator.methodcaller():

from operator import methodcaller

map(methodcaller('function'), list1)

methodcaller() accepts additional arguments that are then passed into the called method; methodcaller('foo', 'bar', spam='eggs')(object) is the equivalent of object.foo('bar', spam='eggs').

If all objects in list1 are the same type or subclasses of that type, and the method you want to call doesn't take any arguments, you can pass in the unbound method to map as the function to call. For example, to lowercase all strings in a list, you can use:

map(str.lower, list_of_strings)

where str.lower is the unbound method on the str type.

Note that a list comprehension is not really the equivalent of a map() here. map() can only do one loop, entirely in C. map() will zip() multiple iterable arguments, and map() in Python 3 is itself an iterator.

A list comprehension on the other hand can do multiple (nested) loops and add in filtering, and the left-hand expression can be any valid Python expression including nested list comprehensions.

like image 101
Martijn Pieters Avatar answered Oct 06 '22 08:10

Martijn Pieters


You could invoke the method directly using the type of the object.

map(lambda x: type(x).function(), list1)

which is not much more exciting than the more straightforward

map(lambda x: x.function(), list1)

However, it does point out that it would be nice if Python had a function composition operator, something like f**g(x) == f(g(x)). Then you could write

map(type**function, list1)
like image 39
chepner Avatar answered Oct 06 '22 07:10

chepner