How to compare these 2 python tuple of tuples of unequal length and output another tuple based on the comparison? -
i have tuple of tuples;
tup1= ( ('aaa', 2), ('bbb', 3) )
i have tuple;
tup2 = ('aaa', 'bbb', 'ccc', 'ddd')
i want compare tup1
, tup2
. based on comparison, want create tuple of tuples this;
outputtup = ( ('aaa', 2), ('bbb', 3), ('ccc', 0), ('ddd', 0) )
the logic this. every element inside tup2 , matching element in tup1. if there matching element(example 'aaa') in tup1, copy outputtup ('aaa', 2). if there no matching element (example 'ccc'), assign value of 0 , append outputtup ('ccc', 0).
how can done in python 2.7? thanks.
this works output want:
tup1 = ( ('aaa', 2), ('bbb', 3) ) tup2 = ('aaa', 'bbb', 'ccc', 'ddd') dic = dict( tup1 ) tri in tup2: dic[tri] = dic.get(tri,0) print tuple(dic.items()) #(('aaa', 2), ('bbb', 3), ('ccc', 0), ('ddd', 0))
Comments
Post a Comment