Andras Palfi Andras Palfi - 7 months ago 36
Python Question

I have two sets of data and I need to build a data structure that organizers them into sets. (Python)

Using Python 2.7. I have one set of data consisting of id tags:

SET1=[{'MISC': u'2759'}, {'MISC': u'2759'}, {'MISC': u'2759'}, {'MISC': u'2758'},{'MISC': u'2758'}, {'MISC': u'1751'}]

and another set consisting of different id tags:

SET2= [u'15672542c8ed280b', u'1566b77702f8865f', u'1565c2241aebb314', u'155c6888c507e365', u'155c5b8ded9a7c03', u'155c1173f58f1494']

As you can see, the sets are one-to-one and each MISC tag relates to the corresponding id in SET2. So for example, the first element in SET1,
{'MISC': u'2759'}
needs to relate to the first element in SET2:

So ideally, I want to build a data structure like so:

Matched_IDS=[{2759, 15672542c8ed280b}, {2759, 1566b77702f8865f} , {2759, 1565c2241aebb314}, {...}, {...} ]

I attempted this approach so far, but since I used two for loops I iterated over the data twice, and get a very ugly looking set:

for a in SET1:
for b in SET2:

Anyone have a more elegant, working solution that they could kindly point me in the right direction towards?

zip([m['MISC'] for m in SET1], SET2)

That should give you what you want I think, assuming your "sets" (they are actually lists) are the same length.