python - 列表转换 - 就地

标签 python arrays list in-place

我正在尝试将 ['1','2','3','4'] 转换为 [1,2,3,4] 我想就地进行此转换。有可能做到吗?如果不是,最佳解决方案是什么。

最佳答案

我觉得用map比较好对于这类任务。这会创建迭代器,这意味着它的内存效率更高。

l = list(map(int, l))
# here I convert it to list, but usually you would just iterate over it
# so you can just do
for item in map(int, l):
    ...

关于python - 列表转换 - 就地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158754/

相关文章:

python - 为什么 any() 方法没有返回我认为应该返回的值?

python - Jinja2 模板继承

python - python 类 __init__ 函数的 PyCharm 模板

javascript - jquery的图像预加载问题

c# - Linq 查询字段是否在数组中?

python - 解析 flask-restful 中的整数列表

python - 访问 Pandas DataFrame 元素中的列表

python - 如何比较列表与列表的不均匀列表

python - 将 txt 文件中的值列表转换为字典

mysql - 在蛋糕模板文件中设置和获取bindModel查询