简体   繁体   English

如何从二维数组中的不同列表中减去具有相同索引的对象

[英]How to subtract objects with the same index from different lists that are within a two-dimensional array

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.我已经阅读了其他一些 Stack Overflow 响应,但到目前为止我看到的大多数响应都是针对 Numpy 或其他语言的。

This is the question that I am trying to solve: https://www.codechef.com/submit/SC31这是我要解决的问题: 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.第一个列表索引 0 中的对象被第二个列表索引 0 减去,依此类推。 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.我也无法打印出此代码的 output 因为它说列表索引超出范围,不知道如何解决这个问题,因为我认为一旦索引超出范围,我的 for 循环将停止迭代,但我猜想情况并非如此,我不确定要更改什么来解决此问题。

If you have any suggestions, I would appreciate it, thanks in advance.如果您有任何建议,我将不胜感激,在此先感谢。

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 row 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(result[i]) - int(row[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]

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]

As said in comments, if you are allowed to use numpy , I'd do:正如评论中所说,如果您被允许使用numpy ,我会这样做:

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) . “第一行减去第二行减去第三行......”是a[0] - a[1] - a[2] -...这是2 * a[0] - a.sum(axis=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM