Home > OS >  convert a duplicated list into a combined one with same key
convert a duplicated list into a combined one with same key

Time:10-21

Can you please help? I have a list: And this list could be very long duplicated of the values can vary because of it.

l1 = [[{'node': 'node-3', 'value': 7.5456592756413645},{'node': 'node-5', 'value': 6.988051860579239},{'node': 'node-0', 'value': 11.394453190010722},{'node': 'node-7', 'value': 7.905077155911794}, {'node': 'node-4', 'value': 7.792379308708253}, {'node': 'node-14', 'value': 10.292450756795946}, {'node': 'node-1', 'value': 6.355755419836891}, {'node': 'node-15', 'value': 14.111596406948182}, {'node': 'node-16', 'value': 13.40657800705202}, {'node': 'node-2', 'value': 6.015374411354142}, {'node': 'node-9', 'value': 7.047300820373079}, {'node': 'node-17', 'value': 12.68578754440751}, {'node': 'node-8', 'value': 7.800883809244761}, {'node': 'node-10', 'value': 12.064107712975112}, {'node': 'node-6', 'value': 9.7540013919274}, {'node': 'node-12', 'value': 12.318039154397544}, {'node': 'node-13', 'value': 9.755403652626981}, {'node': 'node-11', 'value': 12.261621612054046}], [{'node': 'node-3', 'value': 6.761078690857657}, {'node': 'node-5', 'value': 8.749145084411227}, {'node': 'node-0', 'value': 12.366653629672784}, {'node': 'node-7', 'value': 9.646920894631135}, {'node': 'node-4', 'value': 6.496804630916606}, {'node': 'node-14', 'value': 13.52834341167122}, {'node': 'node-1', 'value': 6.624804810314409}, {'node': 'node-15', 'value': 16.68569380522415}, {'node': 'node-16', 'value': 15.114235217020076}, {'node': 'node-2', 'value': 6.957799674666696}, {'node': 'node-9', 'value': 11.003732393553822}, {'node': 'node-17', 'value': 14.872999031713572}, {'node': 'node-8', 'value': 6.1783864922581175}, {'node': 'node-10', 'value': 8.761628593039768}, {'node': 'node-6', 'value': 7.151000108866281}, {'node': 'node-12', 'value': 11.369187124409983}, {'node': 'node-13', 'value': 11.13383409022003}, {'node': 'node-11', 'value': 10.450195830323363}]]

As you can see, it is several nested lists with same key, but different values, I need help to combined into one nested list, something like:

[
{'node': 'node-3', 'value': [ 7.5456592756413645 ,  6.761078690857657, ...]},
{'node': 'node-5', 'value': [ 6.988051860579239 ,  8.749145084411227, ... ]}
....
]

CodePudding user response:

You may achieve this, with varying length N of values, with the following:

l2 = [{'nodes': l1[0][i]['node'], 'value': [l1[n][i]['value'] for n in range(len(l1))]} for i in range(len(l1[0]))]

Something to note, the original post requested for 'value'. This was maintained to meet the original request, but I would encourage you to use a generalized name like 'all_attributes' or 'all_values' instead due to the variable length. This will eliminate future confusion if the 'value' contains more than a single value or 'values' containing to a single value. However, just be sure to document what you decide.

Results:

[
  {'nodes': 'node-3', 'value': [7.5456592756413645, 6.761078690857657]}
  {'nodes': 'node-5', 'value': [6.988051860579239, 8.749145084411227]}
  {'nodes': 'node-0', 'value': [11.394453190010722, 12.366653629672784]}
  {'nodes': 'node-7', 'value': [7.905077155911794, 9.646920894631135]}
  {'nodes': 'node-4', 'value': [7.792379308708253, 6.496804630916606]}
  {'nodes': 'node-14', 'value': [10.292450756795946, 13.52834341167122]}
  {'nodes': 'node-1', 'value': [6.355755419836891, 6.624804810314409]}
  {'nodes': 'node-15', 'value': [14.111596406948182, 16.68569380522415]}
  {'nodes': 'node-16', 'value': [13.40657800705202, 15.114235217020076]}
  {'nodes': 'node-2', 'value': [6.015374411354142, 6.957799674666696]}
  {'nodes': 'node-9', 'value': [7.047300820373079, 11.003732393553822]}
  {'nodes': 'node-17', 'value': [12.68578754440751, 14.872999031713572]}
  {'nodes': 'node-8', 'value': [7.800883809244761, 6.1783864922581175]}
  {'nodes': 'node-10', 'value': [12.064107712975112, 8.761628593039768]}
  {'nodes': 'node-6', 'value': [9.7540013919274, 7.151000108866281]}
  {'nodes': 'node-12', 'value': [12.318039154397544, 11.369187124409983]}
  {'nodes': 'node-13', 'value': [9.755403652626981, 11.13383409022003]}
  {'nodes': 'node-11', 'value': [12.261621612054046, 10.450195830323363]}
]

CodePudding user response:

You can do it with zip

[{'node': i.get('node'), 'values': [j.get('value') for v in j]} for i,j in zip(*l1)]

For the use zip in both sublists, the order should be equal.

Output:

[{'node': 'node-3', 'values': [6.761078690857657, 6.761078690857657]},
 {'node': 'node-5', 'values': [8.749145084411227, 8.749145084411227]},
 {'node': 'node-0', 'values': [12.366653629672784, 12.366653629672784]},
 {'node': 'node-7', 'values': [9.646920894631135, 9.646920894631135]},
 {'node': 'node-4', 'values': [6.496804630916606, 6.496804630916606]},
 {'node': 'node-14', 'values': [13.52834341167122, 13.52834341167122]},
 {'node': 'node-1', 'values': [6.624804810314409, 6.624804810314409]},
 {'node': 'node-15', 'values': [16.68569380522415, 16.68569380522415]},
 {'node': 'node-16', 'values': [15.114235217020076, 15.114235217020076]},
 {'node': 'node-2', 'values': [6.957799674666696, 6.957799674666696]},
 {'node': 'node-9', 'values': [11.003732393553822, 11.003732393553822]},
 {'node': 'node-17', 'values': [14.872999031713572, 14.872999031713572]},
 {'node': 'node-8', 'values': [6.1783864922581175, 6.1783864922581175]},
 {'node': 'node-10', 'values': [8.761628593039768, 8.761628593039768]},
 {'node': 'node-6', 'values': [7.151000108866281, 7.151000108866281]},
 {'node': 'node-12', 'values': [11.369187124409983, 11.369187124409983]},
 {'node': 'node-13', 'values': [11.13383409022003, 11.13383409022003]},
 {'node': 'node-11', 'values': [10.450195830323363, 10.45019583032
  • Related