Save difference between arrays in a new array

Asked

Viewed 36 times

4

The Script below returns the intersection between arrays within a new array, I wanted to save the difference, in case what does not belong, how to modify this script ?

SCRIPT:

values = []

a = [('SRV', 'CLIENT'),('SRV1', 'CLIENT'),('SRV2', 'CLIENT')]
b = [('SRV', 'CLIENT'),('SRV1', 'CLIENT'),('SRV3', 'CLIENT')]

gap = []
for row in a:
    if row[0] not in b:
        gap.append(row)

OUTPUT:

[('SRV', 'CLIENT'), ('SRV1', 'CLIENT')]

VIEW CODE

GOAL

[('SRV2', 'CLIENT'')]

1 answer

6


Using a logic similar to yours we have:

def diff(a, b):
        b = set(b)
        return [row for row in a if row not in b]

print(diff(a,b))

But I think it’s simpler:

gap = list(set(a)-set(b))
print(gap)
  • The logic would be the same if arrays were like this ? [u('SRV', 'CLIENT'),u('SRV1', 'CLIENT'),u('SRV2', 'CLIENT')] the example represents an array filled with a select of a DB.

  • Strange array this way, ai is not array, explains better

  • When I run one go inside the array the output is like this (u'campo',u'campo1',u'campo2')

  • The database is db2,and use the libs ibm_db and ibm_db_dbi

Browser other questions tagged

You are not signed in. Login or sign up in order to post.