如何将函数应用于python中列表的每个子列表?

时间:2015-05-05 18:51:12

标签: python list function python-2.7 python-3.x

假设我有一个这样的列表:

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

我有一个函数假设我想将F函数应用于F函数的每个子列表,可以计算一些关于两个列表的分数。如何将此F函数应用于list_of_lists的每个列表,并在新列表中返回每个分数,如下所示:

new_list = [score_1, score_2, score_3]

我尝试使用以下map函数:

map(F, list_of_lists).append(new_list)

5 个答案:

答案 0 :(得分:5)

您可以使用内置map来执行此操作。

因此,如果您要应用的功能是len,您可以这样做:

>>> 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很大,那么在Python3中使用map会更有意义,因为迭代器会更快。

答案 1 :(得分:3)

您可以使用列表推导,例如

mainThread.join()

例如,

[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] 循环进行迭代并调用该函数。

除此之外,您可以使用Python 2.7中的map函数将函数应用于所有元素并构造列表。例如,

for

但是,map在Python 3.x中返回一个map迭代器对象。因此,您需要将其显式转换为列表,例如

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

您可能想了解Guido对此post>>> 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] 的看法。

基本上,map会更频繁地要求您创建一个新函数(大多数人创建一个map函数)。但在许多情况下,列表理解避免了这一点。

答案 2 :(得分:3)

怎么样

[ F(x) for x in list_of_lists ]

将迭代list_of_lists,以每个子列表作为参数调用F,然后生成结果列表。

如果你想将这些子列表用作F的所有参数,你可以稍微改变一下

[ F(*x) for x in list_of_lists ]

答案 3 :(得分:2)

适用于任意嵌套列表的内容,例如: [[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]]]]

通过避免传递flevels,可以做一些优化 (使递归函数成为包装器中的内部函数, 这样它可以使用来自外部范围的flevels,但这是次要的。 (请注意,这是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()总是生成列表......)

答案 4 :(得分:1)

地图是你的朋友! map接受一个函数和一个iterable(例如list),并在列表的每个元素上应用该函数。

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]]

另一种可能的方法(也来自函数式编程)是列表推导。 List comprehension是一种从Python中的iterable构造列表的方法。语法为[element for element in iterable]。任何计算都可以在元素上完成,所以

[f(element) for element in iterable]

表示结果列表是元素列表,其中每个元素都是函数f的结果。与map一样,list comprehension可以进一步嵌套,从而产生嵌套的元素函数应用程序。

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

输出

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