从阵列中删除阵列

时间:2018-08-29 17:00:13

标签: python numpy

我想从数组A中删除可能在数组B中找到的元素。

例如:

A = numpy.array([1, 5, 17, 28, 5])
B = numpy.array([3, 5])
C = numpy.delete(A, B)
  

C = [1,17,28]

5 个答案:

答案 0 :(得分:7)

Numpy具有此功能:

numpy.setdiff1d(A, B)

这将为您提供预期结果的新数组。

有关sciPy documentation

的更多信息

答案 1 :(得分:4)

您可以尝试:

list(set(A)-set(B))
#[1, 28, 17]

或列表理解:

[a for a in A if a not in B]

另一种解决方案:

import numpy 
A[~numpy.isin(A, B)]
#array([ 1, 17, 28])

答案 2 :(得分:4)

使用通过// create an ordered list document.write("<ol>"); document.write("<li>Reduce spending on non-necessities.</li>") document.write("<li>Use extra money to pay off debt,starting with the highest-interest credit card.</li>") document.write("<li>Continue paying off debts until you are debt free.</li>") document.write ("<li>Put a fixed percent of your pay aside in savings.</li>") document.write ("<li> <a href="https://www.financial-planning.com/"> Financial Planning </a> </li>") document.write("</ol>"); 进行迭代的列表理解,获取不在data = {'0': 25, '1': 35, '10': 29, '11': 28, '12': 17, '13': 33, '14': 31, '15': 25, '16': 30, '17': 38, '18': 26, '19': 26, '2': 20, '20': 31, '21': 29, '22': 22, '23': 32, '24': 32, '25': 27, '26': 27, '27': 28, '28': 23, '29': 186, '3': 26, '4': 30, '5': 27, '6': 28, '7': 34, '8': 32, '9': 38} values = data.values() avg = sum(values)/len(values) disproportionate_data = {key:val for key,val in data.iteritems() if val > threshold+avg} 中的值:

A

答案 3 :(得分:1)

尝试一下

numpy.array([e for e in A if not e in B])

答案 4 :(得分:1)

您也可以尝试:

V= [7,12,8,22,1]
N= [12,22,0,1,80,82,83,100,200,1000]
def test(array1, array2):
    A = array1
    B = array2
    c = []
    for a in range(len(A)):
        boolian=False
        for b in range(len(B)):
            if A[a]==B[b]:
                boolian=True
        if boolian==False:
            c.append(A[a])
    print(c)


test(V,N)