[Tutor] List manipulation

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Wed Sep 13 22:47:47 CEST 2006


Thank you Bob for your email.
Sorry for the confusion. 
here is what I ment:

test = ['10\t15', '16\t20', '25\t35', '45\t50',
'55\t60', '61\t65', '75\t80']


>>> x = []
>>> y = []
>>> for m in test:
...     cols = m.split('\t')
...     x.append(cols[0])
...     y.append(cols[1])
...

>>> x
['10', '16', '25', '45', '55', '61', '75']
>>> y
['15', '20', '35', '50', '60', '65', '80']

>>> for m in range(0,6):
...         k = m+1
...         if int(x[k])-int(y[m])==1:
...                 print x[m]+'\t'+y[k]
...         else:
...                 print x[m]+'\t'+y[m]
...
10      20
16      20 # This is unwanted #####
25      35
45      50
55      65
61      65# This is unwanted #####
75      80 # IS MISSING from result###

16-20 and 61-65 is unwanted, to get rid of these I am
doing these.
My desired result:
10      20
25      35
45      50
55      65
75      80



If I consider the length of the list:
>>> for m in range(0,7):
...         k = m+1
...         if int(x[k])-int(y[m])==1:
...                 print x[m]+'\t'+y[k]
...         else:
...                 print x[m]+'\t'+y[m]
...
10      20
16      20
25      35
45      50
55      65
61      65
Traceback (most recent call last):
  File "<stdin>", line 3, in ?
IndexError: list index out of range




How can I avoid 16-20 and 61-65 and get 75-80 in the
result. 

Also, is there any easy way to do this. 

Thanks







--- Bob Gailer <bgailer at alum.rpi.edu> wrote:

> Srinivas Iyyer wrote:
> > Dear group:
> >
> > I have a data like this:
> > 10      15
> > 16      20
> > 25      35
> > 45      50
> > 55      60
> > 61      65
> > 75      80
> >
> > Since 15 precedes 16, I want to consider 10:20 as
> one
> > unit.  If I repeat completely for data
> >
> > I would get:
> > 10      20
> > 25      35
> > 45      50
> > 55      65
> > 75      80
> >
> > test = ['10\t15', '16\t20', '25\t35', '45\t50',
> > '55\t60', '61\t65', '75\t80']
> >
> >
> > I cannot think a way to do this in simple. Could
> > members suggest some way to solve this please. 
> >   
> I assume by "precedes" you mean is one-less-than. To
> test this you 
> should convert the strings into integers. Since the
> numbers come in 
> pairs each pair must be split at the \t (using
> split), then convert each 
> number to integer (using int).
> 
> Hope that's enough to get you started.
> 
> -- 
> Bob Gailer
> 510-978-4454
> 
> 


__________________________________________________
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