gpt4 book ai didi

python - 列表转换 - 就地

转载 作者:行者123 更新时间:2023-11-28 16:22:30 26 4
gpt4 key购买 nike

我正在尝试将 ['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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com