将列表中的列表中的字符串转换为整数

时间:2018-05-04 07:55:21

标签: python-3.x

unit_cost = open(“/ Users / amit / Downloads / NetworkDesign 3 / small_example / unit_tran_cost.txt”,“r”) ucost = unit_cost.read

unitcostlist_of_lists = []

对于unit_cost中的行

inner_list = [i for i in line.split()]
unitcostlist_of_lists.append(inner_list)

unitcostlist_of_lists

输出是:[['4,16,8,4'],['6,6,6,9'],['12,15,7,8']]

所需的输出是:[['4','16','8','4'],['6','6','6','9'],['12',' 15' , '7', '8']]

我打开的文件如下:

4,16,8,4

6,6,6,9

12,15,7,8

1 个答案:

答案 0 :(得分:0)

我认为你只需要机会split()split(","),即

inner_list = [i for i in line.split(",")]
unitcostlist_of_lists.append(inner_list)

应该做的伎俩。