Reputation: 491
I apologise if this question is badly phrased.
My script is running comparisons on trade messages, written as string values, and I wish to see if there are fields appearing in one messages, but not another. An example of the two messages being used are:
msg1 = ['35=D', '52=20160429', '11=0001A', '44=440', '15=EUR']
msg2 = ['35=D', '52=20160428', '11=0001B', '44=439', '15=EUR']
To compare the lists, I am using:
result = [o for o in a if o not in b]
Right now, my script is providing me with elements that do not exactly match, so for example:
nonmatches = ['52=20160428', '11=0001B', '44=439']
This will not work for me, as the tags are still present in both messages. So my question is - how would I compare the two lists after the first three characters of each element?
Upvotes: 0
Views: 1654
Reputation: 1393
Are you sure that a list representation is the best container for your data? Perhaps you should use a dict
msg1 = ['35=D', '52=20160429', '11=0001A', '44=440', '15=EUR']
msg2 = ['35=D', '52=20160428', '11=0001B', '44=439', '15=EUR']
d1 = {key:value for key,value in [item.split('=',1) for item in msg1]}
d2 = {key:value for key,value in [item.split('=',1) for item in msg2]}
Now your data is stored in a dict
print(d1)
# {'15': 'EUR', '44': '440', '11': '0001A', '35': 'D', '52': '20160429'}
print (d1.keys())
# dict_keys(['15', '44', '11', '35', '52'])
Now you can use set operations which will perform better than @Phillip-Martin 's answer above.
Eg:
# All different keys
d1.keys() ^ d2.keys()
# Keys in d1 not in d2
d1.keys() - d2.keys()
# Keys in d2 not in d1
d2.keys() - d1.keys()
Upvotes: 2
Reputation: 1960
Like @avinash said, use slicing:
result = [o for o in msg1 if o[3:] not in [field[3:] for field in msg2]]
Upvotes: 1
Reputation: 153
I don't really understand what your mean. Do you want find different element in msg1. I provide my answer here.
In [21]: msg1 = ['35=D', '52=20160429', '11=0001A', '44=440', '15=EUR']
In [22]: msg2 = ['35=D', '52=20160428', '11=0001B', '44=439', '15=EUR']
In [23]: [o for o in msg1 if o not in msg2]
Out[23]: ['52=20160429', '11=0001A', '44=440']
Upvotes: -1