[Tutor] efficient method to search between two lists

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Thu Mar 23 08:11:42 CET 2006


Hi Danny, 
Thanks for reminding me the tip from Kent :-)
da = {}
for m in list_a:
        cols = m.split('\t')
        ter = cols[0]
        da.setdefault(ter,[]).append(m)
>>> da
{'S66427': ['S66427\tNM_002892'], 'U05861':
['U05861\tNM_001353'], 'D63483': ['D63483\tNM_145349',
'D63483\tNM_003693', 'D63483\tNM_145351'], 'S68290':
['S68290\tNM_001353'], 'S83513':
['S83513\tNM_001117'], 'S57153':
['S57153\tNM_002892'], 'X60435':
['X60435\tNM_001117'], 'U75370':
['U75370\tNM_005035'], 'D86864': ['D86864\tNM_145349',
'D86864\tNM_003693', 'D86864\tNM_145351']}

>>> for i in list_b:
...         co = i.split('\t')[2]
...         items = da.get(co)
...         for x in items:
...                 print i+'\t'+x
...
HC_G110 1000_at S83513  S83513  NM_001117
HC_G110 1001_at D63483  D63483  NM_145349
HC_G110 1001_at D63483  D63483  NM_003693
HC_G110 1001_at D63483  D63483  NM_145351
HC_G110 1002_f_at       D86864  D86864  NM_145349
HC_G110 1002_f_at       D86864  D86864  NM_003693
HC_G110 1002_f_at       D86864  D86864  NM_145351
HC_G112 1003_s_at       X60435  X60435  NM_001117
HC_G112 1004_at S57153  S57153  NM_002892


Thanks again danny and Kent. 


--- Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:

> > Although dictionary is superfast, due to
> duplications
> > in both columns of list_a, a dictionary option
> falls
> > out.
> 
> Hi Srinivas,
> 
> A dictionary method can work perfectly well.
> 
> The assumption that I think is being made is that
> dictionary values are
> restricted to single values.  But dictionaries are
> more general than this.
> We can make dictionaries that map from strings to
> lists of things.
> 
> For example:
> 
> #######
> sample = { 'a': ['alphabet', 'artichoke',
> 'antelope'],
>            'b': ['beta', 'brocolli',
> 'brontosaurus'],
>            'g': ['gamma', 'grub', 'gnome'] }
> #######
> 
> is a perfectly good dictionary.
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list