如何在三个数组中找到具有相同值的索引

时间:2018-02-23 04:01:21

标签: python arrays numpy

数据形状是:

axis=1

xyz的含义是xyz坐标值。

所以想要在三个数组中获得相同的{% sellername: {{ usuario.sellername }} %} 值。

如何在三个数组中找到具有相同值的索引?

1 个答案:

答案 0 :(得分:-1)

如果 所有数组 长度相同 ,您可以直接找到它们所有3的索引都相同。

例如(python,O(n))

def findMatchingIndex(a, b, c):
    for index, val in enumerate(a): # enumerate through a to get the index of the item we are looking at...
        if val == b[index] and b[index] == c[index]: # if a = b and b = c then a = c
            return index # return the first shared index we find
    return -1 # or -1 for no index.

# for example...
findMatchingIndex([1, 2, 3], [2, 2, 1], [3, 2, 1]) # = 1
findMatchingIndex([1, 2, 3], [4, 5, 6], [3, 2, 1]) # = -1

修改

但是如果长度不同,你会想要使用最小公尺,这是最小长度。

def findMatchingIndex2(a, b, c): 
    # get the smallest size
    size = sorted( [ len(a), len(b), len(c) ] )[0] # sort the lengths of the arrays and get the smallest/first one

    for index in range (0, size): # go until the smallest array is done
        if a[index] == b[index] and b[index] == c[index]: # if a = b and b = c then a = c
            return index # return the first shared index we find
    return -1 # or -1 for no index.

# for example...
findMatchingIndex2([1, 2, 3], [2, 2, 1], [3, 2, 1]) # = 1
findMatchingIndex2([1, 2, 3], [2, 3, 3], [3, 3, 3]) # = 2
findMatchingIndex2([1, 2, 3], [2, 3, 3], [3, 3]) # = -1