Srinivas Iyyer wrote:
> 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?

None is the result of looking up one of the items in imagecls in d. This means 
that item has no entry in d, so d.get() returns None. Then you try to iterate 
over None (in the nested loop) and you can't do that.

I would fix it by using d[i] to do the dictionary lookup and catching KeyError:

for i in imagecls:
  try:
    items= d[i]
    for i in items:
      print i
  except KeyError:
    print 'No entry for', i, 'in d'

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

Some docs here:
http://docs.python.org/lib/typesmapping.html

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to