将列表中的所有字符串转换为 int [英] Convert all strings in a list to int

查看:40
本文介绍了将列表中的所有字符串转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,我想将列表中的所有字符串转换为整数.

In Python, I want to convert all strings in a list to integers.

如果我有:

results = ['1', '2', '3']

我是如何做到的:

results = [1, 2, 3]

推荐答案

使用 map 函数(在 Python 2.x 中):

Use the map function (in Python 2.x):

results = map(int, results)

在 Python 3 中,您需要从 map 转换结果 到一个列表:

In Python 3, you will need to convert the result from map to a list:

results = list(map(int, results))

这篇关于将列表中的所有字符串转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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