lundi 29 novembre 2021

Adding values in a list if the are the same, with a twist

I have a question (python) regarding adding of values if the are the same. So for example we have:

Lst1 = [A, A, B, A, C, D]
lst2 = [1, 2, 3, 4, 5, 6]

what I would like to know is how I can add the numbers in lst2 if the strings in lst1 are similar. the end result would thus be:

new_lst1 = [A, B, A, C, D]
new_lst2 = [3, 3, 4, 5, 6]

where

new_lst2[0] = 1+2

So values only get added when the are next to each other.

To make it more complicated, it should also be possible if we have this example:

lst3 = [A, A, A, B, B, A, A]
lst4 = [1, 2, 3, 4, 5, 6, 7]

of which the result has to be:

new_lst3 = [A, B, A]
new_lst4 = [6, 9, 13]

where new_lst4[0] = 1 + 2 + 3, new_lst4[1] = 4 + 5, and new_lst4[2] = 6 + 7.

Thank you in advance!

for a bit of background: I wrote a code that searches in Dutch online underground models and returns the data of the underground of a specific input location.

The data is made up of layers:
Layer1, Name: "BXz1", top_layer, bottom_layer, transitivity
Layer2, Name: "BXz2", top_layer, bottom_layer, transitivity
Layer3, Name: "KRz1", top_layer, bottom_layer, transitivity

etc..

BXz1 and BXz2 are the same main layer however different sublayers. In terms of transitivity I would like to combine them if they are next to each other. so in that way i would get:
Layer1+2, Name: BX, top_layer1, bottom_layer2, combined transitivity Layer3, Name: "KRz1", top_layer, bottom_layer, transitivity

Aucun commentaire:

Enregistrer un commentaire