How to apply a function to each list sublist in python?

Suppose I have a list like this:

list_of_lists = [['how to apply'],['a function'],['to each list?']]

And I have a function that allows me to say that I want to apply a function Fto each sublist of a function Fthat can calculate some score for about two lists. How to apply this function Fto each list list_of_listsand return each point to a new list as follows:

new_list = [score_1, score_2, score_3]

I tried using the function the mapfollowing:

map(F, list_of_lists).append(new_list)
+4
source share
5 answers

For this you can use the built-in map.

So, if the function you want to apply len, you would do:

>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> map(len, list_of_lists)
[1, 1, 1]

Python3, , list:

>>> map(len, list_of_lists)
<map object at 0x7f1faf5da208>
>>> list(map(len, list_of_lists))
[1, 1, 1]

- , Python2, Python3, - . - :

[apply_function(item) for item in list_of_lists]

Python 2, 3 - .

, list_of_lists , map Python3 , .

+5

,

[function_to_be_done(item) for item in list_of_lists]

,

>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> [len(item) for item in list_of_lists]
[1, 1, 1]

.. , - . , , for .


, map Python 2.7, , ,

>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> map(len, list_of_lists)
[1, 1, 1]

map Python 3.x. , ,

>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> map(len, list_of_lists)
<map object at 0x7f94026afd30>
>>> list(map(len, list_of_lists))
[1, 1, 1]

, Guido map post.

, map ( lambda). .

+3

[ F(x) for x in list_of_lists ]

list_of_lists, F , .

F, ,

[ F(*x) for x in list_of_lists ]
+3

-, , [[1,2], [[5]], [7, [8, [9,11]]]]:

def apply_f(a,f):
 if isinstance(a,list):
     return map(lambda t:apply_f(t,f), a)
 else:
     return f(a)

:

>>> ll=[[1,2],[[5]],[7,[8,[9,11]]]]
>>> apply_f(ll,lambda t:t**2)
[[1, 4], [[25]], [49, [64, [81, 121]]]]

:

def apply_f(a,f,depth,levels):
    if isinstance(a,list):
        return map(lambda t:apply_f(t,f,depth+1,levels), a)
    else:
        if depth in levels:
            return f(a)
        else:
            return a

>>> apply_f(ll,lambda t:t**2, 0, [2,4])
[[1, 4], [[5]], [49, [8, [81, 121]]]]

, f levels ( , f levels ), . ( , Python 2, Python 3 map - ).


, :

def apply_f(a,f):
    try: 
       return(f(a))
    except:
       return map(lambda t:apply_f(t,f), a)

:

>> apply_f([(1,2),[[5]],[7,(8,[9,11])]],lambda t:t**2)
[[1, 4], [[25]], [49, [64, [81, 121]]]]

( , map() ...)

+2

- ! map (, ) .

map(len, [['how to apply'],['a function'],['to each list?']]) 

[1, 1, 1]

, :

map(lambda x: map(lambda y: y + 1, x), [[1], [1, 2], [1, 2, 3]])

[[2], [2, 3], [2, 3, 4]]

( ) - . - Python. [element for element in iterable]. ,

[f(element) for element in iterable]

means that the resulting list will be a list of elements, where each element is the result of the function f. Like a map, a list comprehension can be optionally nested, which leads to the application of nested elements.

[element + 1 for element in el] for el in [[1], [1, 2], [1, 2, 3]]]

Output

[[2], [2, 3], [2, 3, 4]]
+1
source

All Articles