使用int将带有str的列表转换为列表的最简单方法是什么? [英] What is the easiest way to convert list with str into list with int?
本文介绍了使用int将带有str的列表转换为列表的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将带有 str
的列表转换为Python中带有 int
的列表的最简单方法是什么?
例如,我们必须将 ['1','2','3']
转换为 [1,2,3 ]
。当然,我们可以使用进行
循环,但这太容易了。
What is the easiest way to convert list with str
into list with int
in Python?
For example, we have to convert ['1', '2', '3']
to [1, 2, 3]
. Of course, we can use a for
loop, but it's too easy.
推荐答案
Python 2.x:
Python 2.x:
map(int, ["1", "2", "3"])
Python 3.x(在3.x,地图
返回一个迭代器,而不是2.x中的列表):
Python 3.x (in 3.x, map
returns an iterator, not a list as in 2.x):
list(map(int, ["1", "2", "3"]))
这篇关于使用int将带有str的列表转换为列表的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文