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

岁酱吖の 提交于 2021-01-28 07:09:58

问题


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.]]

回答1:


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]])


来源:https://stackoverflow.com/questions/64564575/most-efficient-way-to-merge-lists-where-the-final-values-are-the-max-from-each-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!