Home > Software engineering >  How to subtract objects with the same index from different lists that are within a two-dimensional a
How to subtract objects with the same index from different lists that are within a two-dimensional a

Time:08-07

I've already read some other Stack Overflow responses, but most of the ones I have seen so far are for Numpy or other languages.

This is the question that I am trying to solve: https://www.codechef.com/submit/SC31

I don't know the amount of lines that they will give me until I run it, so I can't just hardcode the amount of lists that I will need beforehand.

Here is what I am trying to do: Let's say I had the two dimensional array:

[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4]

I am trying to subtract the objects from the first list from the second list, and then the updated first list is subtracted by the third list. The result that I am looking for is:

[-1, -2, -3, -4]

The objects from the first list index 0 is subtracted by the second list index 0 and so on. This is done for all the columns in the list.

Here is the code that I have so far but I am having trouble implementing the subtracting part.

for wow in range(0, int(input())):
    me_list = []
    thin = int(input())
    for thing in range(0, thin):
        temp = input().split()
        temp = list(map(int, temp))
        me_list.append(temp)
    good_list = []
    for number in range(0, thin):
        for speed in range(0, 10):
            me_list[number][speed] = me_list[number][speed] - me_list[number 1][speed]
    print(me_list)

I also can't print out the output of this code because it says the list index is out of range, not sure how to fix this because I thought that my for loops would stop iterating once the indexes would become out of range, but I guess that's not the case and I'm not sure what to change to fix this problem.

If you have any suggestions, I would appreciate it, thanks in advance.

CodePudding user response:

I commented in my answer, hope it would help you

rows = int(input("Input your number of rows: "))
columns = int(input("Input your number of columns: "))

result = [] # accumulately save subtracted list
for row_idx in range(0, rows): # loop over each row
    row = input().split(" ") # read data each rrow, each element in rrow is separated by one space
    if len(result) == 0: # check if the first row
        result = row # assign result to the first row
    else: # if result has data
        assert len(result) == len(row) # check if each row has same number of element
        result = [int(row[i]) - int(result[i]) for i in range(len(result))] # subtract the new input row to the accumulated result
print(result)

The input console

Input your number of rows: 3
Input your number of columns: 4
1 2 3 4
1 2 3 4
1 2 3 4

The result

[1, 2, 3, 4]

CodePudding user response:

I hope this helps. This solution just performs the subtraction independently of the number of rows. I am hardcoding the lines but I think you were having problems only with the subtraction process. Let me know in the comments if you need help getting the input from the user.

test_data = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [-4, -3, -2, -1]]

for i in range(1, len(test_data)):
    first_row = test_data[0]
    current_row = test_data[I]
    print(f"First Row {first_row}")
    for j in range(0, len(first_row)):
        first_row[j] -= current_row[j]

    print(f"First_row updated to {first_row}")

Result:

first_row [1, 2, 3, 4]
first_row updated to [0, 0, 0, 0]
first_row [0, 0, 0, 0]
first_row updated to [-1, -2, -3, -4]
first_row [-1, -2, -3, -4]
first_row updated to [3, 1, -1, -3]

CodePudding user response:

As said in comments, if you are allowed to use numpy, I'd do:

a = np.array([[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]])
res = 2 * a[0] - a.sum(axis=0)
>>> res
array([-1, -2, -3, -4])

Explanation:

"first row minus second row minus third row ..." is a[0] - a[1] - a[2] - ... which is 2 * a[0] - a.sum(axis=0).

  • Related