LampShade
LampShade

Reputation: 2775

Most efficient way to merge lists where the final values are the max from each list maintaining position

What's the most efficient way to merge these lists into one single list where the final values are the max from each list maintaining position? Right now I'm doing a brute force iteration over all of the lists and setting the max value in the final list. It works but it's not very efficient since my data sets are massive. Any ideas on how to make this more efficient?

graph1 = [[0, 0, 0], [1, 0, 1], [2, 0, 0]]
graph2 = [[5, 0, 0], [1, 0, 1], [2, 0, 0]]
graph3 = [[2, 1, 0], [0, 0, 1], [0, 0, 0]]
graph4 = [[1, 0, 1], [9, 0, 0], [2, 0, 0]]

graphs = [graph1, graph2, graph3, graph4]

# TODO, what's the most efficient way to merge these lists into one single list where the final values are the max from each list maintaining position?
# desiredResultGraph = [[5, 1, 1], [9, 0, 1], [2, 0, 0]]

Updated solution based on Mark Meyer's solution below:

graph = np.ndarray(shape=(4, 3, 3), dtype=float, order='F')
graph[0] = [[0, 0, 1], [1, 0, 1], [2, 0, 0]]
graph[1] = [[0, 0, 1], [1, 0, 1], [2, 0, 0]]
graph[2] = [[5, 0, 0], [1, 0, 1], [2, 0, 0]]
graph[3] = [[2, 1, 0], [9, 0, 1], [0, 0, 0]]

PrintAndLog("graph of type " + str(type(graph)) + " = " + str(graph))

resultGraph = graph.max(axis=0)
PrintAndLog("resultGraph of type " + str(type(resultGraph)) + " = " + str(resultGraph))

Output:

graph of type <class 'numpy.ndarray'> = 
[[[ 0.  0.  1.]
  [ 1.  0.  1.]
  [ 2.  0.  0.]]

 [[ 0.  0.  1.]
  [ 1.  0.  1.]
  [ 2.  0.  0.]]

 [[ 5.  0.  0.]
  [ 1.  0.  1.]
  [ 2.  0.  0.]]

 [[ 2.  1.  0.]
  [ 9.  0.  1.]
  [ 0.  0.  0.]]]
resultGraph of type <class 'numpy.ndarray'> = 
[[ 5.  1.  1.]
 [ 9.  0.  1.]
 [ 2.  0.  0.]]

Upvotes: 1

Views: 371

Answers (1)

Mark
Mark

Reputation: 92460

If you are manipulating large sets of numeric data, you will be hard-pressed to beat the performance of Numpy. And it makes things like this easy:

import numpy as np

graph1 = [[0, 0, 0], [1, 0, 1], [2, 0, 0]]
graph2 = [[5, 0, 0], [1, 0, 1], [2, 0, 0]]
graph3 = [[2, 1, 0], [0, 0, 1], [0, 0, 0]]
graph4 = [[1, 0, 1], [9, 0, 0], [2, 0, 0]]

np.array([graph1, graph2, graph3, graph4]).max(axis = 0)

Result:

array([[5, 1, 1],
       [9, 0, 1],
       [2, 0, 0]])

Upvotes: 4

Related Questions