將列表中的所有字符串轉(zhuǎn)換為int在Python中,我希望將列表中的所有字符串轉(zhuǎn)換為整數(shù)。所以如果我有:results = ['1', '2', '3']我該怎么做?results = [1, 2, 3]
3 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超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
提交
取消