简体   繁体   English

Python:在python中找到两个数组之间的差异

[英]Python : find difference between two arrays in python

I have two arrays in python.我在 python 中有两个数组。

arr1 = [(7, 0.78, 7920), (8, 0.9, 9000)]
arr2 = [(7, 1.68, 8460)]

In this array first value is ID (7,8,7).I want result basis on ID .在这个数组中,第一个值是ID (7,8,7)。我想要基于ID结果。 If ID same then get difference between others two value.如果 ID 相同,则获得其他两个值之间的差异。
Between arr1 and arr2 ID 7 is same then will be subtraction (1.68-0.78 = 0.9) And (8460-7920 = 540) otherwise it will be same. arr1arr2 ID 7 相同,则减去 (1.68-0.78 = 0.9) 和 (8460-7920 = 540) 否则将相同。 How to get Result like that ?如何得到这样的结果?

diffArray = [(7, 0.9, 540), (8, 0.9, 9000)]

Here's one way of doing it.这是一种方法。 First, I'm creating dictionaries from ID to the other values:首先,我正在创建从 ID 到其他值的字典:

arr1 = [(7, 0.78, 7920), (8, 0.9, 9000)]
arr2 = [(7, 1.68, 8460)]

dict1 = {i: (x, y) for i, x, y in arr1}
dict2 = {i: (x, y) for i, x, y in arr2}

Then I'm using a list comprehension with a condition to do the right thing:然后我使用带有条件的列表理解来做正确的事情:

diffArray = [
    (
        i,
        abs(dict2[i][0] - dict1[i][0]),
        abs(dict2[i][1] - dict1[i][1]),
    )
    if i in dict2
    else (i, dict1[i][0], dict1[i][1])
    for i in dict1
]

The result is maybe a bit surprising:结果可能有点出人意料:

[(7, 0.8999999999999999, 540), (8, 0.9, 9000)]

the apparent imprecision is due to the floating point representation.明显的不精确是由于浮点表示。 To get around that (if it is important), use Decimal or round the values.为了解决这个问题(如果它很重要),请使用Decimal或舍入这些值。

This will go through every element in arr2 then add it into the correct index of arr1.这将遍历 arr2 中的每个元素,然后将其添加到 arr1 的正确索引中。 If it doesnt exist then it will add it in fresh如果它不存在,那么它会以新鲜的方式添加它

arr1 = [(7, 0.78, 7920), (8, 0.9, 9000)]
arr2 = [(7, 1.68, 8460), (6,1,1)]

for j in arr2:
    for ix, i in enumerate(arr1):
        if i[0] == j[0]:
            arr1[ix] = (i[0], j[1]-i[1], j[2]-i[2])
            break
    else:
        arr1.append(j)
arr1

[(7, 0.8999999999999999, 540), (8, 0.9, 9000), (7, 1.68, 8460), (6, 1, 1)] [(7, 0.8999999999999999, 540), (8, 0.9, 9000), (7, 1.68, 8460), (6, 1, 1)]

arr1 = [(7, 0.78, 7920), (8, 0.9, 9000)]
arr2 = [(7, 1.68, 8460)]
arr1.sort(key=lambda x:x[0])
arr2.sort(key=lambda x:x[0])
from itertools import zip_longest
new_list = []
for value1, value2 in zip_longest(arr1, arr2):
    if value2:
        _id1, val3, val4 = value1
        _id2, val5, val6 = value2
        if _id1 == _id2:
            diff1 = val5 - val3
            diff2 = val6 - val4
            new_list.append((_id1, diff1, diff2))
    else:
        new_list.append(value1)
print(new_list)
>>>[(7, 0.8999999999999999, 540), (8, 0.9, 9000)]

Here is my attempt, using two list comprehensions:这是我的尝试,使用两个列表推导式:

arr1 = [(7, 0.78, 7920), (8, 0.9, 9000)]
arr2 = [(7, 1.68, 8460)]

common = [tuple([a[0], b[1] - a[1], b[2] - a[2]]) for a in arr1 for b in arr2 if a[0]==b[0]]
others = [a for a in arr1 for b in arr2 if a[0]!=b[0]]
result = common + others

Output:输出:

[(7, 0.8999999999999999, 540), (8, 0.9, 9000)]

Here's one way of doing it.这是一种方法。

def diffArrays(arr1, arr2): def diffArrays(arr1, arr2):

"""
Parameters
----------
arr1 : array
    firt array.
arr2 : array
    second array to be compared with.

Returns
-------
arr3: array
    array containing only the different elements.
"""
arr3 = []
[arr3.append(el) if el not in arr2 else '' for el in arr1]
[arr3.append(el) if el not in arr1 else '' for el in arr2]

return arr3

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

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