[Tutor] a dictionary method good for this process

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Sun Aug 7 19:01:29 CEST 2005


Hi Kent:
Thank you for your tip on making a sub-dictionary. 
However, I see some new prbs. that I am unable to
solve are persisting. could you enlighten me, please. 

> d={}
> for m in listB:
>   cols = m.split('\t')
>   term = cols[1]
>   d.setdefault(term, []).append(m)
>   
> for i in listA:
>     items = d.get(i)
>     for item in items:
>        print item

>>> for i in imagecls:
	items= d.get(i)
	for i in items:
		print i

		

Traceback (most recent call last):
  File "<pyshell#169>", line 3, in -toplevel-
    for i in items:
TypeError: iteration over non-sequence



>>> for i in imagecls:
	items = d.get(i)
	print items

	
None
['NM_001903\t21652\tT65187\n',
'NM_001903\t21652\tT65118\n']
['NM_001659\t22012\tT66053\n']
None





First when I loop over items, I get iterative over
non-sequence (I assume this is None type object). 

But when I print then I see the results as a list and
None. 

What is this None. And why is it not allowing me to
iteratve over the list?

Please throw some light.  Also, .setdefault() is still
some what murky. May be I need to work more on that. 

Thanks
Srini



--- Kent Johnson <kent37 at tds.net> wrote:

> Srinivas Iyyer wrote:
> > Dear group:
> > 
> > I have two lists and I have to match the element
> in
> > the first list to element in second list and print
> the
> > line from second list:
> > 
> > Example:
> > listA =['apple','baby','cat']
> > listB
> =['fruit\tapple','tree\tapple','fruit\tmango',
> >         'human\tbaby'
> >         'infant\tbaby'
> >         'human\tAlbert'
> >         'animal\tcat'
> >         'tiger\tcat'
> >         'animan\tdog']
> > 
> > 
> > I have to take apple, search in listB and print
> > 
> > fruit  apple
> > tree apple
> > infant baby
> > human baby
> > animal cat
> > tiger cat
> 
> Presumably that is the results from searching for
> 'apple', 'baby' and 'cat'...
> 
> I would make a helper dictionary that maps from the
> second element in listB to a list of listB entries
> containing that element. This way you make just one
> pass over listB:
> 
> listA =['apple','baby','cat']
> listB =['fruit\tapple','tree\tapple','fruit\tmango',
>         'human\tbaby',
>         'infant\tbaby',
>         'human\tAlbert',
>         'animal\tcat',
>         'tiger\tcat',
>         'animan\tdog']
> 
> # Make a helper dict that maps the second element of
> listB to a list of elements
> d={}
> for m in listB:
>   cols = m.split('\t')
>   term = cols[1]
>   d.setdefault(term, []).append(m)
>   
> for i in listA:
>     items = d.get(i)
>     for item in items:
>        print item
> 
> 
> The only tricky part is the line
>   d.setdefault(term, []).append(m)
> 
> This is just a shortcut for something like
>   try:
>     data = d[term]
>   except KeyError:
>     d[term] = data = []
>   data.append(m)
> 
> Kent
> 
> > 
> > 
> > I am doing it this way:
> > 
> > for i in listA:
> >    for m in listB:
> >         cols = m.split('\t')
> >         term = cols[2]
> >         if i == term:
> >            print m
> > 
> > this is very time consuming.
> > 
> > Because the two columns in listB are redundant I
> am
> > unable to do a dict method.
> > 
> > The other way could be using a reg.ex method
> (which I
> > did not try yet because the script written based
> on
> > equality is still running.
> > 
> > for i in listA:
> >        pat = re.compile(i)
> >        for m in listB:
> >              cols = m.split('\t')
> >              terms = cols[1]
> >              if pat.match(terms):
> >                      print m
> > 
> > Experts, could you please suggest any other method
> > which is fast and does the job correctly. 
> > 
> > Thank you.
> > 
> > Srini
> > 
> > 
> > 
> > 
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam
> protection around 
> > http://mail.yahoo.com 
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



		
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 


More information about the Tutor mailing list