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

``````IN      OUT       SECTION       PARTIAL        ADDITIONAL          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.

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!

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 `0`s.)

``````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