Lists operations and searches

Hello community, I have a problem that I still can’t find a way to solve, I have 4 lists as information [[IN], [OUT], [SECTION], [PARTIAL]], and I need to perform the following calculations, in ADDITIONAL I must add them all the sections that in their accumulated are connected to the main section, and in ACCUMULATED add PARTIAL plus ADDITIONAL if any and the previous ACCUMULATED.

A1      A2         A1-A2           1                0                    1
A2      A3         A2-A3          0.5               0                   1.5
A3      A4         A3-A4           0               7.5                   9
A4      A5         A4-A5           2                0                   11
A5      A6         A5-A6           1                1                   13
A3.1   A3.2      A3.1-A3.2         1                0                    1
A3.2   A3.3      A3.2-A3.3         3                3                    7
A3.3    A3        A3.3-A3          0                0                    7
A3.1'   A3       A3.1'-A3         0.5               0                   0.5
A5.1    A5       A5.1-A5           1                0                    1
A3.2.1  A3      A3.2.1-A3          3                0                    3

How would you solve this in Python?

Attached is an image of a table with a simple exercise, I hope you understand it. In any case, I will also place another image that describes it graphically.


What is this for?

By the way, your last line of values has typos: A3 should be A3.2 for the numbers and picture to match.

IN = ["A1", "A2", "A3", "A4", "A5", "A3.1", "A3.2", "A3.3", "A3.1'", "A5.1", "A3.2.1"] OUT = ["A2", "A3", "A4", "A5", "A6", "A3.2", "A3.3", "A3", "A3", "A5", "A3.2"] PARTIAL = [1, 0.5, 0, 2, 1, 1, 3, 0, 0.5, 1, 3] length = min(len(IN), len(OUT), len(PARTIAL)) ACCUMULATED = [0] * length ADDITIONAL = [0] * length for i in range(length): in_value = IN[i] out_value = OUT[i] current = PARTIAL[i] to_additional = 0 # store between iterations j = i # add current to relevant stuff in path: while in_value in IN: j = IN.index(in_value) out_value = OUT[j] ACCUMULATED[j] += current ADDITIONAL[j] += to_additional if (in_value.count(".") > out_value.count(".")): to_additional = current # will be added to ADDITIONAL[j] in next iteration else: to_additional = 0 #print(in_value, end=" > ") in_value = out_value # set up for next iteration # end of inner while loop #print(in_value) ADDITIONAL[j] += to_additional print("IN =", IN) print("OUT =", OUT) print("PARTIAL =", PARTIAL) print("ADDITIONAL =", ADDITIONAL) print("ACCUMULATED =", ACCUMULATED)

I think that creating an object or class or using nodes in a linked list (that merges with other linked lists) to make a reverse tree type thing may be a better approach.

1 Like

Thank you very much!

I’m a Civil Engineer and I have to do that by hand in excel, so I just wanted to automated with python because I’m learning python.
I only have a month learning python,
How do I get to your level of knowledge, I admire you a lot!
Congratulations you are a pro! :clap:t3: :clap:t3: :clap:t3:

Please explain to me why you multiply by zero the length of the lists you are creating on lines 6 and 7?

And would this code work if the rows were out of order? that is, if after the row of A1-A2, comes the row of A3.3-A3 and then the row of A2-A3 and so on, I refer to the rows as the table that I present in my question with all the data that correspond to it.

Yes, that will work when the rows are in a different order.
[I’ve been learning Python and other languages and doing some small projects or challenges for about a year.]

You know that you can combine lists using + to make a new list
If you want to repeat a sequence in a list, you can use * similarly. (I looked this up online, because I wanted to make a new list of a specific size, filled with 0s.)

list1 = [1, 2] + [3, 4]
# list1 is [1, 2, 3, 4]
list2 = [0] * 3
# list2 is [0, 0, 0] 

Also, lots of trial and error.

1 Like