0

I have two sets

a[i]={'aaa@','bb','ccc@'}
b[j]={'aaa','bb@','ccc@'}

I want to compare each string in a[i] with b[j] such that if both strings are same and they have special character at end then it prints "equal" like ccc@ in above lists are same if strings are equal but one of them have a special character then it displays "Not completely matched"

abd
  • 73
  • 1
  • 8
  • 1
    You have *sets*, not lists. – Martijn Pieters Mar 13 '15 at 14:41
  • yes so how to compare using sets – abd Mar 13 '15 at 14:43
  • @abd, could you specify what string do you want to compare with what other string? You see, it is a problem that you have sets instead of lists, because sets are not ordered and there is no way to tell, which element of the first set you want to compare to which element of the second set. – bpgergo Mar 13 '15 at 14:48
  • I want to compare all elements of first set with all elements of other set – abd Mar 13 '15 at 14:49
  • I have strings which are words like first set {"Product invoice","product verification","product completed@"} and second set {"Product invoice","product verification@","product completed@"} – abd Mar 13 '15 at 14:52

2 Answers2

1

an exampel with list:

a=['aaa@','bb','ccc@']
b=['aaa','bb@','ccc@']

index = 0
print "ordered comparison:"
for i,j in zip(a,b):
    if i == j:
        print str(index) + ": Equal"
    elif i.replace("@","") == j.replace("@",""):
        print str(index) + ": Not completely Matched"
    else:
        print str(index) + ": Different"
    index+=1

print "\nunordered comparison:"
for x in a:
    for y in b:
        if x == y:
            print x,y + " are Equal"
        elif x.replace("@","") == y.replace("@",""):
            print x,y + " Not completely Matched"
        else:
            print x,y + " Different"

Output:

enter image description here

0

Sets are easily compared element by element:

>>> a={'aaa@','bb','ccc@'}
>>> b={'aaa','bb@','ccc@'}
>>> c=a.copy()
>>> a-b
set(['aaa@', 'bb'])
>>> a-c
set([])

>>> d={"Product invoice","product verification","product completed@"}
>>> e= {"Product invoice","product verification@","product completed@"}
>>> d-e
set(['product verification'])
>>> d^e
set(['product verification@', 'product verification'])

Then use the truthiness of an empty or non empty set to get what you want:

>>> 'not matched' if a-b else 'matched'
'not matched'
>>> 'not matched' if a-c else 'matched'
'matched'
dawg
  • 98,345
  • 23
  • 131
  • 206