Python3中利用map将列表当中的string类型转换为int类型或其它类型

看下面的代码

list1 = ['11','22']
list1 = map(int, list1)
print(type(list1[0]))

编译后会报错TypeError: 'map' object is not subscriptable,在百度了后才知道,上面代码在python2当中才是正确的,在py3中,它返回的是迭代器,不是我们直接想要的list。
所以正确的做法如下:

list1 = ['11','22']
list1 = list(map(int, list1))
print(type(list1[0]))
发布了51 篇原创文章 · 获赞 67 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/solitudi/article/details/104182866