将列表中的所有字符串转换为int在Python中,我希望将列表中的所有字符串转换为整数。所以如果我有:results = ['1', '2', '3']我该怎么做?results = [1, 2, 3]
3 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
def str_list_to_int_list(str_list): n = 0 while n < len(str_list): str_list[n] = int(str_list[n]) n += 1 return(str_list)
>>> results = ["1", "2", "3"] >>> str_list_to_int_list(results) [1, 2, 3]
def str_list_to_int_list(str_list): int_list = [int(n) for n in str_list] return int_list
添加回答
举报
0/150
提交
取消