The*_*mer 6 python nested-lists python-2.7 data-structures python-3.x
我在 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']]
Run Code Online (Sandbox Code Playgroud)
使用几种方法转换数据时:
1.
new_data_list = [[int(x) for x in list] for list in data]
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
ValueError:int() 的无效文字以 10 为基数:''
我的数据列表中没有非数字数据。但是标题可能有一些东西,比如一个空标题被视为非数字值,因为我已经从 csv 创建了一个数据列表。
我想知道一种有效的方法,可以将列表的每个元素转换为 int,同时保留数据多列表。
调用int每个嵌套列表中的每个项目:
new_list = [[int(x) for x in lst] for lst in nested]
Run Code Online (Sandbox Code Playgroud)
或使用map:
new_list = [list(map(int, lst)) for lst in nested]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7655 次 |
| 最近记录: |