Python:就地地图 [英] Python: map in place

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

问题描述

我想知道是否有一种方法可以在某些东西上运行地图.映射的工作方式是需要迭代,并将函数应用于该迭代中的每个项目,从而产生一个列表.有没有办法让地图修改可迭代对象本身?

I was wondering if there is a way to run map on something. The way map works is it takes an iterable and applies a function to each item in that iterable producing a list. Is there a way to have map modify the iterable object itself?

推荐答案

如果需要在适当的位置修改列表,通常可以使用切片分配

A slice assignment is often ok if you need to modify a list in place

mylist[:] = map(func, mylist)

这篇关于Python:就地地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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