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

后端 未结 5 1328
南笙
南笙 2020-12-02 00:34

Lets say I have a list like this:

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

And I have a function let\'s sa

5条回答
  •  遥遥无期
    2020-12-02 00:47

    How about

    [ F(x) for x in list_of_lists ]
    

    which will iterate over list_of_lists, call F with each sublist as an argument, then generate a list of the results.

    If you want to use the sublists as all the arguments to F you could do it slightly differently as

    [ F(*x) for x in list_of_lists ]
    

提交回复
热议问题