需要处理大量数据,数据格式为二维动态列表,数据总共有200行,由4个50行的数据块组成。需要以块为单位对数据进行索引,并在每个块内以行/列的方式访问数据。
解决方案
方法一:使用map函数和列表解析
- 将数据列表转换为列表的列表
- 使用map函数将每个子列表中的元素转换为整数
- 使用列表解析将转换后的子列表存储在新的列表中
l = [['1', '4', '4', '244', '263', '704', '952'],
['2', '4', '4', '215', '172', '305', '33'],
['3', '4', '4', '344', '279', '377', '1945'],
['4', '4', '4', '66', '79', '169', '150'],
['5', '4', '3', '16', '22', '247'],
['6', '4', '4', '17', '154', '93', '309'],
['7', '3', '2', '233', '311'],
['8', '3', '1', '15'],
['9', '3', '2', '55', '102']]
intList = [map(int, sublist) for sublist in l]
print(intList)
输出:
[[1, 4, 4, 244, 263, 704, 952],
[2, 4, 4, 215, 172, 305, 33],
[3, 4, 4, 344, 279, 377, 1945],
[4, 4, 4, 66, 79, 169, 150],
[5, 4, 3, 16, 22, 247],
[6, 4, 4, 17, 154, 93, 309],
[7, 3, 2, 233, 311],
[8, 3, 1, 15],
[9, 3, 2, 55, 102]]
方法二:使用orthogonalize函数正交化数据
- 计算列表中所有子列表的最大长度
- 对于每个子列表,如果长度小于最大长度,则在末尾填充0
- 正交化后的数据可以按块、行和列进行索引
def orthogonalize(li):
max_col = max(len(x) for x in li) + 1
for l in li:
for i in range(max_col-len(l)):
l.append(0)
li = [[1, 4, 4, 244, 263, 704, 952],
['2', '4', '4', '215', '172', '305', '33'],
['3', '4', '4', '344', '279', '377', '1945'],
['4', '4', '4', '66', '79', '169', '150'],
['5', '4', '3', '16', '22', '247'],
['6', '4', '4', '17', '154', '93', '309'],
['7', '3', '2', '233', '311'],
['8', '3', '1', '15'],
['9', '3', '2', '55', '102']]
orthogonalize(li)
print(li)
输出:
[[1, 4, 4, 244, 263, 704, 952, 0],
[2, 4, 4, 215, 172, 305, 33, 0],
[3, 4, 4, 344, 279, 377, 1945, 0],
[4, 4, 4, 66, 79, 169, 150, 0],
[5, 4, 3, 16, 22, 247, 0, 0],
[6, 4, 4, 17, 154, 93, 309, 0],
[7, 3, 2, 233, 311, 0, 0, 0],
[8, 3, 1, 15, 0, 0, 0, 0],
[9, 3, 2, 55, 102, 0, 0, 0]]
方法三:使用getData函数按块、行和列获取数据
- 定义getData函数,函数的参数包括数据列表、块号、行号和列号(可选)
- 如果没有指定列号,则返回指定块和行的数据
- 如果指定了列号,则返回指定块、行和列的数据
def getData(data, block, line, column = None):
"""
Index start from 0 for block, line and column
getData(data, 0,1,1)
=> block# is 0 it will be processed as is
=> it will read value of line#1, column#1
getData(data, 1,1,1)
=> block# is 1 it will be convert to line number = 50*(block)+line
=> it will read value of line#51, column#1
"""
if column is None:
return data[50*(block)+line]
else:
return data[50*(block)+line][column]
d = [[1, 4, 4, 244, 263, 704, 952, 0],
[2, 4, 4, 215, 172, 305, 33, 0],
[3, 4, 4, 344, 279, 377, 1945, 0],
............
[51, 4, 4, 244, 263, 704, 952, 0],
[52, 4, 4, 215, 172, 305, 33, 0],
[53, 4, 4, 344