【发布时间】:2018-05-09 16:36:43
【问题描述】:
我在 python 中有嵌套列表的数据,其中一部分看起来像:
data = [['214', '205', '0', '14', '710', '1813494849', '0'], ['214', '204', '0', '30', '710', '1813494856', '0'], ['214', '204', '0', '34', '710', '1813494863', '0'], ['213', '204', '0', '35', '710', '1813494870', '0'], ['213', '203', '0', '35', '710', '1813494877', '0']]
在使用几种方法转换数据时:
1.
new_data_list = [[int(x) for x in list] for list in data]
-
list=[] for i in range(0,len(data)): list.append([]) for j in range(0,len(data[i])): b=int(data[i][j]) list[i].append(b)我收到错误消息:
ValueError: int() 以 10 为底的无效文字:''
我的数据列表中没有非数字数据。但是标题可能有一些东西,比如一个空标题被视为非数字值,因为我从 csv 创建了一个数据列表。
我想知道一种有效的方法,可以将列表的每个元素转换为 int,同时保留数据多列表。
【问题讨论】:
-
旁注:从不 shadow 内置
list,例如请改用L或list_。
标签: python python-3.x python-2.7 data-structures nested-lists