Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何将函数应用于列表的每个子列表?_Python_List_Function_Python 2.7_Python 3.x - Fatal编程技术网

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

在python中,如何将函数应用于列表的每个子列表?,python,list,function,python-2.7,python-3.x,Python,List,Function,Python 2.7,Python 3.x,假设我有这样一个列表: list_of_lists = [['how to apply'],['a function'],['to each list?']] new_list = [score_1, score_2, score_3] 我有一个函数,比如说我想把F函数应用到F函数的每个子列表中,可以计算出两个列表的分数。如何将此F函数应用于每个list\u/u列表中的每个列表,并在新列表中返回每个分数,如下所示: list_of_lists = [['how to apply'],['a

假设我有这样一个列表:

list_of_lists = [['how to apply'],['a function'],['to each list?']]
new_list = [score_1, score_2, score_3]
我有一个函数,比如说我想把
F
函数应用到
F
函数的每个子列表中,可以计算出两个列表的分数。如何将此
F
函数应用于每个
list\u/u列表中的每个列表
,并在新列表中返回每个分数,如下所示:

list_of_lists = [['how to apply'],['a function'],['to each list?']]
new_list = [score_1, score_2, score_3]
我使用
map
功能尝试了以下操作:

map(F, list_of_lists).append(new_list)
您可以使用内置接口来完成此操作

因此,如果要应用的函数是
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中工作,无需任何更改


但是,如果您的列表的输入列表很大,那么在Python3中使用
map
会更有意义,因为迭代器会快得多。

您可以使用列表理解,如下所示

[function_to_be_done(item) for item in list_of_lists]
>>> 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]
比如说,

>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> [len(item) for item in list_of_lists]
[1, 1, 1]
>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> map(len, list_of_lists)
[1, 1, 1]
注意:虽然列表理解看起来像是将函数应用于所有元素的一种方式,但它的主要目的是构造一个新的列表。因此,如果您不想构造一个新列表,那么只需使用
for
循环进行迭代并调用该函数即可


除此之外,还可以使用Python2.7中的函数,将函数应用于所有元素并构造一个列表。比如说,

>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> [len(item) for item in list_of_lists]
[1, 1, 1]
>>> list_of_lists = [['how to apply'],['a function'],['to each list?']]
>>> map(len, list_of_lists)
[1, 1, 1]
但是,在Python3.x中返回一个映射迭代器对象。所以,您需要显式地将其转换为列表,如下所示

[function_to_be_done(item) for item in list_of_lists]
>>> 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]
>>列表的列表=[['如何应用'],['函数'],['到每个列表?']]
>>>地图(列名,列表中的列表)
>>>列表(地图(列名,列表中的列表))
[1, 1, 1]

您可能想在本文中了解Guido对
map
的看法

基本上,
map
更经常地要求您创建一个新函数(大多数人创建一个
lambda
函数)。但在许多情况下,列表理解可以避免这种情况。

怎么样

[ F(x) for x in list_of_lists ]
它将遍历列表中的列表,用每个子列表作为参数调用F,然后生成一个结果列表

如果您想使用子列表作为
F
的所有参数,您可以根据需要稍微改变一下

[ F(*x) for x in list_of_lists ]

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

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

[1, 1, 1]
[[2], [2, 3], [2, 3, 4]]
[[2], [2, 3], [2, 3, 4]]
如果要对子列表的元素进行更精细的计算,可以嵌套贴图:

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

[1, 1, 1]
[[2], [2, 3], [2, 3, 4]]
[[2], [2, 3], [2, 3, 4]]
另一种可能的方法(也来自函数式编程)是列表理解。列表理解是从Python中的iterable构造列表的一种方法。语法是
[iterable中元素对应的元素]
。任何计算都可以在元素上进行,因此

[f(element) for element in iterable]
意味着结果列表将是元素列表,其中每个元素都是函数f的结果。与映射一样,列表理解可以进一步嵌套,从而生成嵌套的元素函数应用程序

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

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

在任意嵌套列表上工作的东西,例如[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
级别
环绕(使递归函数成为包装器中的内部函数, 因此,它可以使用外部范围中的
f
级别
),但这是次要的。(请注意,这是Python2,对于Python3,您需要用其他内容替换
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()
总是生成列表…

高效地执行此操作的最佳方法是什么?列表没有什么特别之处<代码>映射或列表理解将迭代列表中的元素,无论这些元素是
int
s、字符串还是其他列表。感谢Guysorry给出的惊人答案,我想昨晚我接受了@mu無问题是列表列表,解决方案适用于这些列表。如果您正在寻找通用解决方案,请提出一个新问题:)恶心:
map(lambda x:map(lambda y:y+1,x),[1],[1,2],[1,2,3])
。。。pythonic=assembly您能在用户指定的级别或级别列表上进行映射吗?是的,这很简单:您需要将apply\u f的嵌套级别存储在其参数中,并在递归调用中增加它;返回f(a)或a本身,条件是嵌套级别。给你。如果我可以从事咨询工作,并且在几年前通过了GoogleFoobar的LOL测试:-)迪玛,谢谢,会测试的。我是“pythonic”的新手,但迫不及待地想回到高级语言,而不是汇编级的sh*。您刚刚实现的是我喜欢使用的系统中构建的,它一般适用于所有表达式,而不仅仅是列表。van Rossum是一个小丑。你可以通过在[5]中测试Iterable来让它更通用:从集合中导入Iterable In[6]:isinstance([1,2],Iterable)Out[6]:在[7]中为真:isinstance((1,2),Iterable)Out[7]:真