用python将[row,column,value]列表求和为2维列表
我有一个[行索引、列索引、值]列表。行和列可以重复。我想把这个列表总结成一个矩阵式的列表 比如说,用python将[row,column,value]列表求和为2维列表,python,Python,我有一个[行索引、列索引、值]列表。行和列可以重复。我想把这个列表总结成一个矩阵式的列表 比如说, list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]] matrix_style_list = get_matrix_style_list(list_of_list) print(matrix_style_list) [[2, 0], [3, 4]] 在上面的示例中,0 x 0的值是2,因为list\u of_list包含[0,0,1],[0,0
list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
matrix_style_list = get_matrix_style_list(list_of_list)
print(matrix_style_list)
[[2, 0], [3, 4]]
在上面的示例中,
0 x 0
的值是2
,因为list\u of_list
包含[0,0,1],[0,0,1]
和1+1=2
构建一个字典,将每个子列表的最后一个值添加到对应于子列表前两个值元组键的值:
>>> list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
>>> d = {}
>>> for item in list_of_list:
... *location, datum = item
... location = tuple(location)
... d[location] = d.get(location, 0) + datum
...
>>> d
{(1, 0): 3, (0, 0): 2, (1, 1): 4}
查找矩阵的大小:
>>> rows = max(item[0] for item in list_of_list)
>>> columns = max(item[1] for item in list_of_list)
然后,您可以创建具有以下理解的矩阵结构:
>>> structure = [[d.get((row, column), 0) for column in range(columns+1)] for row in range(rows+1)]
>>> structure
[[2, 0], [3, 4]]
构建一个字典,将每个子列表的最后一个值添加到对应于子列表前两个值的元组键的值中:
>>> list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
>>> d = {}
>>> for item in list_of_list:
... *location, datum = item
... location = tuple(location)
... d[location] = d.get(location, 0) + datum
...
>>> d
{(1, 0): 3, (0, 0): 2, (1, 1): 4}
查找矩阵的大小:
>>> rows = max(item[0] for item in list_of_list)
>>> columns = max(item[1] for item in list_of_list)
然后,您可以创建具有以下理解的矩阵结构:
>>> structure = [[d.get((row, column), 0) for column in range(columns+1)] for row in range(rows+1)]
>>> structure
[[2, 0], [3, 4]]
解决这个问题的一个简单方法是,首先使用
max()
函数找出输出数组的形状,然后创建一个包含所有0
的该形状的矩阵,然后继续将列表中的值与该输出矩阵求和。范例-
list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
import operator
def get_matrix_style_list(lol):
x_shape,y_shape = max(lol, key=operator.itemgetter(0,1))[0:2]
output_matrix = [[0 for _ in range(y_shape+1)] for _ in range(x_shape +1)]
for x,y,val in lol:
output_matrix[x][y] += val
return output_matrix
演示-
>>> list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
>>> import operator
>>> def get_matrix_style_list(lol):
... x_shape,y_shape = max(lol, key=operator.itemget
... output_matrix = [[0 for _ in range(y_shape+1)]
... for x,y,val in lol:
... output_matrix[x][y] += val
... return output_matrix
...
>>> get_matrix_style_list(list_of_list)
[[2, 0], [3, 4]]
解决这个问题的一个简单方法是,首先使用max()
函数找出输出数组的形状,然后创建一个包含所有0
的该形状的矩阵,然后继续将列表中的值与该输出矩阵求和。范例-
list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
import operator
def get_matrix_style_list(lol):
x_shape,y_shape = max(lol, key=operator.itemgetter(0,1))[0:2]
output_matrix = [[0 for _ in range(y_shape+1)] for _ in range(x_shape +1)]
for x,y,val in lol:
output_matrix[x][y] += val
return output_matrix
演示-
>>> list_of_list = [[0,0,1], [0,0,1], [1,0,3], [1,1,4]]
>>> import operator
>>> def get_matrix_style_list(lol):
... x_shape,y_shape = max(lol, key=operator.itemget
... output_matrix = [[0 for _ in range(y_shape+1)]
... for x,y,val in lol:
... output_matrix[x][y] += val
... return output_matrix
...
>>> get_matrix_style_list(list_of_list)
[[2, 0], [3, 4]]
为什么[[2,3],[0,4]]
?@gefei,谢谢。我更新了问题。应该是[[2,0],[3,4]]
对吗?@AnandSKumar哦,对不起。这是我的错…为什么[[2,3],[0,4]
?@gefei,谢谢。我更新了问题。应该是[[2,0],[3,4]]
对吗?@AnandSKumar哦,对不起。这是我的错…我只是太懒了,没法写list\u/list
,所以我把它缩写为:)我只是太懒了,没法写list\u/list
,所以我把它缩写为:)