是否有相当于python中的“map”的就地内容? [英] Is there an in-place equivalent to 'map' in python?

查看:131
本文介绍了是否有相当于python中的“map”的就地内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Python:地图

我有一个字符串列表我需要消毒我有一种方法来消毒它们,所以我可以这样做:

I have a list of strings that I need to sanitize. I have a method for sanitizing them, so I could just do:

new_list = map(Sanitize, old_list)

但我不需要保留旧列表。这让我想知道是否有等同于地图的就地等价物。很容易为它编写一个for循环(或一个自定义的就地映射方法),但是是否有内置的东西?

but I don't need to keep the old list around. This got me wondering if there's an in-place equivalent to map. Easy enough to write a for loop for it (or a custom in-place map method), but is there anything built in?

推荐答案

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.

这篇关于是否有相当于python中的“map”的就地内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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