如何获取不包含在另一个数组中的数组?

huangapple go评论98阅读模式
英文:

How to get the arrays not contained in another array?

问题

  1. first = [[0,10],[0,11],[0,12],[0,13]]
  2. second = [[0,10],[0,11]]
  3. # 返回第一个数组中不在第二个数组中的元素
  4. def difference(arr1, arr2):
  5. result = [item for item in arr1 if item not in arr2]
  6. return result
  7. # 调用函数并输出结果
  8. difference(first, second)
  9. # 返回 [[0, 12], [0, 13]]
英文:

I have two Python arrays with arrays inside:

  1. first = [[0,10],[0,11],[0,12],[0,13]]
  2. second = [[0,10],[0,11]]

and I want to get as a result the array that is in the first one, but not in the other:

  1. difference(first,second)
  2. #returns [[0,12],[0,13]]

right now they are numpy arrays, but a solution with regular ones is also valid.

I tried using np.setdiff1d but it returned an array with the exclusive ints, not exclusive arrays.
And I tried to iterate through the second array deleting its elements from the first one:

  1. diff = first.view()
  2. for equal in second:
  3. diff = np.delete(diff, np.where(diff == equal))

but it returned similar not useful results

答案1

得分: 1

你可以使用普通的 Python 列表(而不是 numpy 数组)和简单的列表推导来完成这个任务:

  1. diff = [lst for lst in first if lst not in second]
  2. diff
  3. [[0, 12], [0, 13]]
英文:

You could do this with regular old Python lists (not numpy arrays) and a simple list comprehension:

  1. >>> diff = [lst for lst in first if lst not in second]
  2. >>> diff
  3. [[0, 12], [0, 13]]

答案2

得分: 0

使用NumPy,您可以进行广播以比较元素,并使用np.all / np.any将比较结果合并到掩码中:

  1. import numpy as np
  2. first = np.array([[0,10],[0,11],[0,12],[0,13]])
  3. second = np.array([[0,10],[0,11],[1,7]])
  4. mask = ~np.any(np.all(first[:,None]==second,axis=-1),axis=-1)
  5. print(first[mask])
  6. [[ 0 12]
  7. [ 0 13]]
英文:

Using numpy, you can broadcast to compare the elements and use np.all/np.any to consolidate the comparison results into a mask:

  1. import numpy as np
  2. first = np.array([[0,10],[0,11],[0,12],[0,13]])
  3. second = np.array([[0,10],[0,11],[1,7]])
  4. mask = ~np.any(np.all(first[:,None]==second,axis=-1),axis=-1)
  5. print(first[mask])
  6. [[ 0 12]
  7. [ 0 13]]

答案3

得分: -1

如果我理解正确,您需要一个函数,请尝试这个:

  1. first = [[0,10],[0,11],[0,12],[0,13]]
  2. second = [[0,10],[0,11]]
  3. def difference(first, second): # 声明函数
  4. lst = [] # 声明列表
  5. for i in first: # 循环运行
  6. if i not in second:
  7. lst.append(i) # 仅记住这种方法
  8. return lst # 此函数的输出 'lst'
  9. difference(first, second)

我不确定这是否是最佳选项,但从您的提问方式来看,这种方法适合您。

英文:

If I understand correctly, you need a function, try this:

  1. first = [[0,10],[0,11],[0,12],[0,13]]
  2. second = [[0,10],[0,11]]
  3. def difference(first,second): #declare function
  4. lst = [] #declare list
  5. for i in first: #run loop
  6. if i not in second: lst.append(i) #just remember method
  7. return(lst) #output of this function 'lst'
  8. difference(first,second)

I'm not sure if this is the best option, but looking by the way you ask, this method is for you

huangapple
  • 本文由 发表于 2023年5月29日 01:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定