Re: [Tutor] List manipulation

2006-09-15 Thread Srinivas Iyyer
Dear Kent and Bob,
thank you for your solutions. It helped, however,
based on your suggestions, I intended to solve a
chromosome walking problem. I posted my question on
subject name:
'Limitation of range() function in Walking problem'. 

Thanks again. 
Sri

--- Kent Johnson <[EMAIL PROTECTED]> wrote:

> Srinivas Iyyer wrote:
> > 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']
> 
> >>> I would get:
> >>> 10  20
> >>> 25  35
> >>> 45  50
> >>> 55  65
> >>> 75  80
> 
> Here is my take on it:
> 
> test = ['10\t15', '16\t20', '25\t35',
> '45\t50','55\t60', '61\t65', '75\t80']
> 
> pairs = [ map(int, x.split('\t')) for x in test ]
> 
> i = iter(pairs)
> 
> last = i.next()
> 
> for current in i:
>  if current[0] == last[1]+1:
>  last = [last[0], current[1]]
>  else:
>  print last
>  last = current
> 
> print last
> 
> 
> You can also wrap this in a generator which yields
> the desired pairs, so 
> they can be printed or put in a list or whatever:
> 
> def compress(pairs):
>  i = iter(pairs)
> 
>  last = i.next()
> 
>  for current in i:
>  if current[0] == last[1]+1:
>  last = [last[0], current[1]]
>  else:
>  yield last
>  last = current
> 
>  yield last
> 
> 
> for pair in compress(pairs):
>  print pair
> 
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List manipulation

2006-09-14 Thread Kent Johnson
Srinivas Iyyer wrote:
> 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']

>>> I would get:
>>> 10  20
>>> 25  35
>>> 45  50
>>> 55  65
>>> 75  80

Here is my take on it:

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

pairs = [ map(int, x.split('\t')) for x in test ]

i = iter(pairs)

last = i.next()

for current in i:
 if current[0] == last[1]+1:
 last = [last[0], current[1]]
 else:
 print last
 last = current

print last


You can also wrap this in a generator which yields the desired pairs, so 
they can be printed or put in a list or whatever:

def compress(pairs):
 i = iter(pairs)

 last = i.next()

 for current in i:
 if current[0] == last[1]+1:
 last = [last[0], current[1]]
 else:
 yield last
 last = current

 yield last


for pair in compress(pairs):
 print pair


Kent

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


Re: [Tutor] List manipulation

2006-09-13 Thread Bob Gailer
try this:

 >>> test = ['10\t15', '16\t20', '25\t35', '45\t50','55\t60', '61\t65', 
'75\t80']
 >>> t='\t'.join(test).split('\t')
 >>> t
['10', '15', '16', '20', '25', '35', '45', '50', '55', '60', '61', '65', 
'75', '80']
 >>> t2=[int(i) for i in t]
 >>> t2
[10, 15, 16, 20, 25, 35, 45, 50, 55, 60, 61, 65, 75, 80]
 >>> l= t2[0]
 >>> for i in range(1,len(t2)-1,2):
... if t2[i+1]-t2[i]>1:
... print l, '\t', t2[i]
... l = t2[i+1]
10 20
25 35
45 50
55 65
 >>> print l, '\t', t2[-1]
75 80

-- 
Bob Gailer
510-978-4454

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


Re: [Tutor] List manipulation

2006-09-13 Thread Srinivas Iyyer
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 "", 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 <[EMAIL PROTECTED]> 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 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List manipulation

2006-09-13 Thread Bob Gailer
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

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


[Tutor] List manipulation

2006-09-13 Thread Srinivas Iyyer
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. 

thanks




__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor