[Tutor] Difference between for i in range(len(object)) and for i in object

kumar s ps_python at yahoo.com
Thu Dec 9 17:50:46 CET 2004


Dear group, 
  

My Tab delimited text looks like this:

HG-U95Av2	32972_at	432	117
HG-U95Av2	32972_at	499	631
HG-U95Av2	32972_at	12	185
HG-U95Av2	32972_at	326	83
HG-U95Av2	32972_at	62	197


I want to capture: columns 2 and 3 as tab delim. text:


Here is my code:
>>> spot_cor=[]
>>> for m in cor:
...     cols = split(cor,'\t')
...     spot_cor.append(cols[2]+'\t'+cols[3])
...
...
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
    return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

Here is 2nd way:


>>> test_cor=[]
>>> for m in cor:
...     cols = split(cor,'\t')
...     x = (cols[2]+'\t'+cols[3])
...     test_cor.append(x)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
  File "/usr/local/lib/python2.3/string.py", line 121,
in split
    return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'



Here is my 3rd way of doing this thing:
>>> for m in range(len(cor)):
...     cols = split(cor[m],'\t')
...     spot_cor.append(cols[2]+'\t'+cols[3])
...
>>>
>>> len(spot_cor)
2252
>>>



My question:
 Many people suggested me to avoid  iteration over  a
object using (range(len)) its index and use instead
'Python's power' by using for i in object, instead. 

However, when I tried that using some data, as
demonstrated above, I get error because append method
does not work on list.  In method 2, i tried to append
an object instead of string elements. In both ways the
execution failed because  'List object has no
attribute split'.


Can you help me making me clear about his dogma. 


Thank you. 

Kumar.



--- Guillermo Fernandez Castellanos
<guillermo.fernandez.castellanos at gmail.com> wrote:

> Cheers,
> 
> I think your mistake is here:
>                 if x == y:
>                        for ele3 in spot_int:
>                                if y in ele3:
>                                       
> out.write(ele3)
>                                       
> out.write('\n')
> Each time you find an element that is the same
> (x==y) you don't write
> only y, you write *all* the elements that are in
> spot_init instead
> only the matching one! And it's not what you are
> looking for! :-)
> 
> I'll also change a bit your code to make it look
> more "pythonic" :-)
> 
> > for ele1 in spot_cor:
> >         for ele2 in spot_int:
> >                 cols = split(ele2,'\t')
> >                 y = (cols[0]+'\t'+cols[1])
> >                 if ele1 == y:
> >                         for ele3 in spot_int:
> >                                 if y in ele3:
> >                                        
> out.write(ele3)
> >                                        
> out.write('\n')
> 
> What changes I did:
> 
> for ele1 in range(len(spot_cor)):
>        x = spot_cor[ele1]
> 
> can be writen like:
> for ele1 in spot_cor:
>     x = ele1
> 
> Furthermore, as you only use x once, I changed:
>  if x == y:
> 
> with
> if ele1 == y:
> 
> and deleted the line:
> x = ele1
> 
> I also don't understand why you do this:
> cols = split(ele2,'\t')
> y = (cols[0]+'\t'+cols[1])
> 
> It seems to me that you are separating something to
> put it again
> together. I don't really see why...
> 
> Enjoy,
> 
> Guille
> 



		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250


More information about the Tutor mailing list