Le Fri, 6 Feb 2009 12:30:31 +0530,
jitendra gupta <jitu.ic...@gmail.com> a écrit :

> > #BEGIN
> > my_input = "one two three four five six seven eight nine ten"
> > text = my_input.split()
> > for i,v in enumerate(text):
> >     line =  text[i-3], text[i-2], text[i-1], v, text[i+1], text[i+2],
> > text[i+3]
> >     print line
> > # END

You do not need enumerate in that case:

offset = 3      # constant
my_input = "one two three four five six seven eight nine ten"
words = my_input.split()
for i in range(offset, len(words)-offset):
        # beware of right-open range:
        slice =  [word for word in words[i-offset : i+offset+1]]
        print i,slice
==>
3 ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
4 ['two', 'three', 'four', 'five', 'six', 'seven', 'eight']
5 ['three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
6 ['four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

Adapt using a 'guard' based on offset's value if you actually want all lines.

Denis
------
la vida e estranya
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to