将地图与多个参数一起使用 [英] Using map with multiple args

查看:65
本文介绍了将地图与多个参数一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的 map 可以采用多个可迭代对象,以便在可调用对象可以接受相同数量的输入参数时使用.如果输入可迭代项的长度相同,则其行为类似于列表理解,并传递压缩参数,例如:

 >>>iterables =垃圾邮件",鸡蛋">>>地图(最大,*可迭代)['s,'p','g','s']>>>[zip(* iterables)中的max(* a)]['s,'p','g','s'] 

当输入参数的长度不同时,它会变得很奇怪-Python 2(docs )用 None 填充,但是Python 3(解决方案

关于为什么 map 在python3中被截断,这仅仅是因为python3的 map 实际上是 itertools.imap .并且文档说:

类似于 map(),但在最短的迭代次数用尽时停止而不是填写 None 来缩短可迭代项.原因区别在于,无限迭代器参数通常是 map()的错误(因为对输出进行了完全评估),但是代表为参数提供通用且有用的方式 imap() .

通过截断,您可以执行诸如 map(func,itertools.repeat(5),[1,2,3])之类的操作,并且无需担心就可以遍历结果.如果使用旧的 map ,那将是一个无限循环.

python3中最重要的变化之一是,许多内置函数现在返回生成器而不是 list s,包括 map zip .这种增加的惰性"改变了使用这些功能的方式,从而改变了行为.

我不知道为什么会使用python2的多重可迭代对象来 map .当然,这是类似(在python3中)的快捷方式:

 列表(itertools.starmap(函数,itertools.zip_longest(* iterables))) 

这可能有一些特殊情况,但我从未见过使用过.可能大多数人甚至都不知道 map 接受一系列可迭代对象.因此,AFAIK没有使用多个参数派生任何超能力.

关于为什么 map 是这种语言的原因,这是因为 map 早于列表理解.在进行列表理解之前,它对于构建列表非常有用.并没有为了向后兼容而删除它,因为实际上很多人喜欢它,尽管Guido did 希望将其删除.

要了解有关 map filter reduce 以及其他功能方面的历史的更多信息,请阅读:

Python's map can take multiple iterables, for use when the callable can accept the same number of input arguments. If the input iterables are the same length, thats behaving like the list comprehension passing in zipped arguments, e.g.:

>>> iterables = 'spam', 'eggs'
>>> map(max, *iterables)
['s', 'p', 'g', 's']
>>> [max(*a) for a in zip(*iterables)]
['s', 'p', 'g', 's']

When the input arguments are different length, it gets weird - Python 2 (docs) pads with None, but Python 3 (docs) truncates to shortest iterable.

>>> map(max, 'spam', 'potato')  # 2.x
['s', 'p', 't', 'm', 't', 'o']
>>> list(map(max, 'spam', 'potato'))  # 3.x
['s', 'p', 't', 'm']

Why is this feature existing, what's an example of a typical case where that's needed or useful? I don't know a lot about functional styles, could I be missing out on some great strengths of map that are related to the multiple arguments? And what's the rationale for the API change in 3.x?

解决方案

Regarding why map truncates in python3, this is simply because python3's map is actually itertools.imap. And the documentation says:

Like map() but stops when the shortest iterable is exhausted instead of filling in None for shorter iterables. The reason for the difference is that infinite iterator arguments are typically an error for map() (because the output is fully evaluated) but represent a common and useful way of supplying arguments to imap().

The truncation allows you to do things like map(func, itertools.repeat(5), [1,2,3]) and iterate over the result without worries. With the old map that would be an infinite loop.

One of the most significant changes in python3 is that a lot of built-in functions now return generators instead of lists, including map and zip. This "increased lazyness" changed the way these functions should be used and thus the modified behaviour.

As to why one would ever use python2's multiple-iterables to map I don't know. Sure, it's a shortcut for something like (in python3):

list(itertools.starmap(function, itertools.zip_longest(*iterables)))

This might have some corner case usage, but I've never seen it used. Probably most people don't even know that map accepts a sequence of iterables. So, AFAIK there isn't any super-power deriving from the use of multiple arguments.

As to why map is in the language, that's because map was there long before list-comprehensions. Before list-comprehensions it was quite useful for building lists. It wasn't remove for backward compatibility and because many people actually like it, although Guido did want to remove it.

To learn more about the history of map, filter and reduce and other functional aspects, read: The History of Python: Origins of Python's "Functional" Features

这篇关于将地图与多个参数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆