[Tutor] extracting numbers from a list

Ziad Rahhal ziad.rahhal at gmail.com
Mon Oct 16 23:41:43 CEST 2006


On 10/16/06, kumar s <ps_python at yahoo.com> wrote:
>
> hi :
>
> I have a simple question to ask tutors:
>
> list A :
>
> a = [10,15,18,20,25,30,40]
>
> I want to print
> 10 15 (first two elements)
> 16 18 (16 is last number +1)
> 19 20
> 21 25
> 26 30
> 31 40
>
> >>> fx = a[0]
> >>> fy = a[1]
> >>> b = a[2:]
> >>> ai = iter(b)
> >>> last = ai.next()
> >>> for j in ai:
> ...     print fy+1,last
> ...     last = j
> ...
> 16 18
> 16 20
> 16 25
> 16 30
>
>
> can any one help please.
>
> thank you



  a = [10,15,18,20,25,30, 40]

  print a[0], a[1]

  for i in xrange(2, len(a)):
     print a[i-1] + 1, a[i]

__________________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061016/564616b7/attachment.htm 


More information about the Tutor mailing list