且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有相当于python中的“map”的就地内容?

更新时间:2023-11-28 23:51:40

p>答案是简单的:否。

The answer is simply: no.

答案是,所以我想我会把它放在那里。

Questions of the form "does XXX exist" never tend to get answered directly when the answer is no, so I figured I'd put it out there.

大多数itertools帮助和内建操作在通用迭代器上。 地图过滤器,列表推导, for - 他们所有这些都在迭代器上工作,而不是修改原始容器(如果有的话)。

Most itertools helpers and builtins operate on generic iterators. map, filter, list comprehensions, for--they all work on iterators, not modifying the original container (if any).

为什么这个类别中没有任何突变功能?因为没有通用的通用方式来向容器分配关键字和值的值。例如,基本的dict迭代器( for {} 中的x)遍历密钥,并且赋给一个dict将使用dict的结果作为[]的参数。另一方面,列表遍历,赋值使用隐式索引。底层一致性不是提供这样的泛型函数,所以在 itertools 或内置函数中没有这样的东西。

Why aren't there any mutating functions in this category? Because there's no generic, universal way to assign values to containers with respect to their keys and values. For example, basic dict iterators (for x in {}) iterate over the keys, and assigning to a dict uses the result of the dict as the parameter to []. Lists, on the other hand, iterate over the values, and assignment uses the implicit index. The underlying consistency isn't there to provide generic functions like this, so there's nothing like this in itertools or in the builtins.

他们可以提供它作为 list dict 的方法,但目前他们没有。你只需要自己滚动。

They could provide it as a methods of list and dict, but presently they don't. You'll just need to roll your own.