简体   繁体   English

如何比较 python 中的两个 arrays?

[英]How do I compare two arrays in python?

Suppose I have two arrays假设我有两个 arrays

array1=np.array([1,2,3,4,5,6,7,8,9,10])
array2=np.array([11,1,2,4,10,60,0,3,20,33])

I want to compare the two arrays and store the values that are bigger.我想比较两个 arrays 并存储更大的值。 I want to write a code that will check the first element of array1 with the first element of array2 and store the greatest value between them in a new array and so on.我想编写一个代码来检查 array1 的第一个元素和 array2 的第一个元素,并将它们之间的最大值存储在一个新的数组中,依此类推。

I tried it with this code我用这段代码试过了

array3=[]
i=0
while i<=len(array1):
    if array1[i]>array2[i]:
        array3.append(i)

However, the code doesn't give any output and keeps on running.但是,代码没有给出任何 output 并继续运行。 The two array in question that i want to work with are very big so will a normal loop method work for big arrays as well?我要使用的两个数组非常大,所以正常的循环方法也适用于大 arrays 吗?

If you're using numpy, using numpy all the way is preferable.如果您使用的是 numpy,则最好一直使用 numpy。 It will be faster, and use less memory, than the iterative approach (and is more readable, too).与迭代方法相比,它会更快,并且使用更少的 memory(并且更具可读性)。

np.max([array1, array2], axis=0)

Your loop is infinite because your i never changes.你的循环是无限的,因为你的i永远不会改变。 If you fix that (either by adding i += 1 or by using for i in range(0, len(array1)): ), you will only be appending elements where array1 one is larger, and leaving out any pairs where they are equal, or where the array2 one is larger: an else: would be needed.如果您解决了这个问题(通过添加i += 1或通过使用for i in range(0, len(array1)): ),您将只附加array1较大的元素,并忽略它们所在的任何对相等,或者array2一个更大的else: :。

You're probably looking for something like numpy.maximum :您可能正在寻找类似numpy.maximum的东西:

import numpy as np

array1 = np.array([ 1, 2, 3, 4,  5,  6, 7, 8, 9,  10])
array2 = np.array([11, 1, 2, 4, 10, 60, 0, 3, 20, 33])

np.maximum(array1, array2)
# array([11,  2,  3,  4, 10, 60,  7,  8, 20, 33])

A couple of things:有几件事:

First, the reason it keeps going is that you are not incrementing your iterator i .首先,它继续运行的原因是您没有增加迭代器i

Second, currently you are adding the index of the greater number, and not the greater number.其次,目前您正在添加更大数字的索引,而不是更大的数字。 Try the following code(pure python):尝试以下代码(纯python):

array3 = []
i = 0
while i < len(array1):
    if array1[i] > array2[i]:
        array3.append(array1[i])
    else:
        array3.append(array2[i])
    i += 1

This should work!这应该工作!

import numpy as np

array1=np.array([1,2,3,4,5,6,7,8,9,10])
array2=np.array([11,1,2,4,10,60,0,3,20,33])

array3=[]
i=0
while i<len(array1):
    if array1[i]>array2[i]:
        array3.append(array1[i])
    else:
        array3.append(array2[i])
    i += 1

print(array3)

在此处输入图像描述

Wanted to do this with a list comprehension.想通过列表理解来做到这一点。 This would work assuming the arrays are always the same size.假设 arrays 的大小始终相同,这将起作用。

[max([array1[i],array2[i]]) for i in range(len(array1))]

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

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