Re: [Tutor] confusing enumerate behavior

2009-02-06 Thread عماد نوفل
On Fri, Feb 6, 2009 at 6:54 AM, Kent Johnson  wrote:

> 2009/2/6 jitendra gupta :
>
> > Try this if u r looking for this kind of solution
> my_input = "one two three four five six seven eight nine ten"
> text = my_input.split()
> for i in range(len(text)):
> >if i+3>=len(text):
> >print text[i-3:len(text):1]
> >elif i<=2:
> >print text[0:i+4]
> >else:
> >print text[i-3:i+4]
>
> You can simplify this using the min() and max() functions:
> for i in range(len(text)):
> print text[max(0, i-3):min(i+3, len(text))]
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Thank you all for the great solutions. I'm really grateful, but also still
confused about indexing now. Why is it that if i = 0 indexing starts with
-3.  I never read this before, and I thus assume it is an advanced thing, or
why is it not in elementary to intermediate resources ( The ones I used at
least). Can somebody please suggest an accessible tutorial on this?
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington


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


Re: [Tutor] confusing enumerate behavior

2009-02-06 Thread Kent Johnson
2009/2/6 jitendra gupta :

> Try this if u r looking for this kind of solution
my_input = "one two three four five six seven eight nine ten"
text = my_input.split()
for i in range(len(text)):
>if i+3>=len(text):
>print text[i-3:len(text):1]
>elif i<=2:
>print text[0:i+4]
>else:
>print text[i-3:i+4]

You can simplify this using the min() and max() functions:
for i in range(len(text)):
print text[max(0, i-3):min(i+3, len(text))]

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


Re: [Tutor] confusing enumerate behavior

2009-02-06 Thread spir
Le Fri, 6 Feb 2009 12:30:31 +0530,
jitendra gupta  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


Re: [Tutor] confusing enumerate behavior

2009-02-05 Thread jitendra gupta
On Fri, Feb 6, 2009 at 5:43 AM, Emad Nawfal (عماد نوفل)
 wrote:
> Hi Tutors,
> I'm a little, actually a lot confused by the behavior of the enumerate
> function here. I have a text and I want to get each word within the context
> of the three preceding and the three following words.  I tried this:
> #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
> The ouput was not as I expected. It did not start from the beginning
> (actually I had expected it to throw and exception immediately)
> ('eight', 'nine', 'ten', 'one', 'two', 'three', 'four')
> ('nine', 'ten', 'one', 'two', 'three', 'four', 'five')
> ('ten', 'one', 'two', 'three', 'four', 'five', 'six')
> ('one', 'two', 'three', 'four', 'five', 'six', 'seven')
> ('two', 'three', 'four', 'five', 'six', 'seven', 'eight')
> ('three', 'four', 'five', 'six', 'seven', 'eight', 'nine')
> ('four', 'five', 'six', 'seven', 'eight', 'nine', 'ten')
> Traceback (most recent call last):
>   File "enumerate.py", line 13, in 
> line =  text[i-3], text[i-2], text[i-1], v, text[i+1], text[i+2],
> text[i+3]
> IndexError: list index out of range
> e...@emad-laptop:~/Desktop$
>
> I then though of adding dummy words to the beginning and the end and exclude
> them later like this:
> #BEGIN
> my_input = "one two three four five six seven eight nine ten"
>
> text2 = " nothing " *6 + my_input + " nothing "* 6
>
> text2 = text2.split()
> for i,v in enumerate(text2[6:-6]):
> line =  text2[i-3], text2[i-2], text2[i-1], v, text2[i+1], text2[i+2],
> text2[i+3]
> print line
> #END
>
> The output this time was even more confusing:
> e...@emad-laptop:~/Desktop$ python enumerate.py
> ('nothing', 'nothing', 'nothing', 'one', 'nothing', 'nothing', 'nothing')
> ('nothing', 'nothing', 'nothing', 'two', 'nothing', 'nothing', 'nothing')
> ('nothing', 'nothing', 'nothing', 'three', 'nothing', 'nothing', 'nothing')
> ('nothing', 'nothing', 'nothing', 'four', 'nothing', 'nothing', 'one')
> ('nothing', 'nothing', 'nothing', 'five', 'nothing', 'one', 'two')
> ('nothing', 'nothing', 'nothing', 'six', 'one', 'two', 'three')
> ('nothing', 'nothing', 'nothing', 'seven', 'two', 'three', 'four')
> ('nothing', 'nothing', 'one', 'eight', 'three', 'four', 'five')
> ('nothing', 'one', 'two', 'nine', 'four', 'five', 'six')
> ('one', 'two', 'three', 'ten', 'five', 'six', 'seven')
>
> Can somebody please explain what is going on here? Have I done something
> wrong? How can this be fixed?
>
> Thanks in anticipation,
> Emad
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


hello Emad

Try this if u r looking for this kind of solution
>>>my_input = "one two three four five six seven eight nine ten"
>>>text = my_input.split()
>>>for i in range(len(text)):
if i+3>=len(text):
print text[i-3:len(text):1]
elif i<=2:
print text[0:i+4]
else:
print text[i-3:i+4]

Output is...
['one', 'two', 'three', 'four']
['one', 'two', 'three', 'four', 'five']
['one', 'two', 'three', 'four', 'five', 'six']
['one', 'two', 'three', 'four', 'five', 'six', 'seven']
['two', 'three', 'four', 'five', 'six', 'seven', 'eight']
['three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
['four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
['five', 'six', 'seven', 'eight', 'nine', 'ten']
['six', 'seven', 'eight', 'nine', 'ten']
['seven', 'eight', 'nine', 'ten']


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


Re: [Tutor] confusing enumerate behavior

2009-02-05 Thread Alan Gauld


"Emad Nawfal (عماد نوفل)"  wrote

I'm a little, actually a lot confused by the behavior of the 
enumerate


Its not enumerate thats confusing you its indexing.


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],


for i=0 the first index is -3

but an index of -3 in Python is the third item
from the end. Its a little bit like the list was circular,
so, instead of falling off the end with -1, you wrap
around to the last item.


('eight', 'nine', 'ten', 'one', 'two', 'three', 'four')


So eight is text[-3] because its 3 from the end.


I then though of adding dummy words



text2 = " nothing " *6 + my_input + " nothing "* 6
text2 = text2.split()
for i,v in enumerate(text2[6:-6]):
   line =  text2[i-3], text2[i-2], text2[i-1], v, text2[i+1], 
text2[i+2],


by using the slice you extracted your old list and got the
same set of indexes! Instead of subtracting 3 you would
need to add six minus the offset:

line = text2[i+6-3], text2[i+6-2]
by the way doesn't this look like it could be in a loop?

The other way is to simply test the index and if its beyond
the range of your list add 'nothing'. Something like:

my_input = "one two three four five six seven eight nine ten"
text = my_input.split()
for i,v in enumerate(text):
for n in range(3,0,-1)
   if  i-n < 0:
 print 'nothing',
print text[i],
for n in range(1,4)
   if i+n >= len(text)
print 'nothing,

Its late and this is untested but it should give a starting point.
My brain is saying something about list comprehensions
but I'm too tired to care :-)

Or another waty might be to use your text2 approach and
just print using indexes starting at the offset of your frst entry.
Actually that sounds like it might be the best way...

text2 = 'nil'*3+text+'niil*3
for i in range(3,len(text)):
   print text2[i-3:i+3]

or somesuch...

HTH

Alan G.
Zz


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


[Tutor] confusing enumerate behavior

2009-02-05 Thread عماد نوفل
Hi Tutors,
I'm a little, actually a lot confused by the behavior of the enumerate
function here. I have a text and I want to get each word within the context
of the three preceding and the three following words.  I tried this:
#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
The ouput was not as I expected. It did not start from the beginning
(actually I had expected it to throw and exception immediately)
('eight', 'nine', 'ten', 'one', 'two', 'three', 'four')
('nine', 'ten', 'one', 'two', 'three', 'four', 'five')
('ten', 'one', 'two', 'three', 'four', 'five', 'six')
('one', 'two', 'three', 'four', 'five', 'six', 'seven')
('two', 'three', 'four', 'five', 'six', 'seven', 'eight')
('three', 'four', 'five', 'six', 'seven', 'eight', 'nine')
('four', 'five', 'six', 'seven', 'eight', 'nine', 'ten')
Traceback (most recent call last):
  File "enumerate.py", line 13, in 
line =  text[i-3], text[i-2], text[i-1], v, text[i+1], text[i+2],
text[i+3]
IndexError: list index out of range
e...@emad-laptop:~/Desktop$

I then though of adding dummy words to the beginning and the end and exclude
them later like this:
#BEGIN
my_input = "one two three four five six seven eight nine ten"

text2 = " nothing " *6 + my_input + " nothing "* 6

text2 = text2.split()
for i,v in enumerate(text2[6:-6]):
line =  text2[i-3], text2[i-2], text2[i-1], v, text2[i+1], text2[i+2],
text2[i+3]
print line
#END

The output this time was even more confusing:
e...@emad-laptop:~/Desktop$ python enumerate.py
('nothing', 'nothing', 'nothing', 'one', 'nothing', 'nothing', 'nothing')
('nothing', 'nothing', 'nothing', 'two', 'nothing', 'nothing', 'nothing')
('nothing', 'nothing', 'nothing', 'three', 'nothing', 'nothing', 'nothing')
('nothing', 'nothing', 'nothing', 'four', 'nothing', 'nothing', 'one')
('nothing', 'nothing', 'nothing', 'five', 'nothing', 'one', 'two')
('nothing', 'nothing', 'nothing', 'six', 'one', 'two', 'three')
('nothing', 'nothing', 'nothing', 'seven', 'two', 'three', 'four')
('nothing', 'nothing', 'one', 'eight', 'three', 'four', 'five')
('nothing', 'one', 'two', 'nine', 'four', 'five', 'six')
('one', 'two', 'three', 'ten', 'five', 'six', 'seven')

Can somebody please explain what is going on here? Have I done something
wrong? How can this be fixed?

Thanks in anticipation,
Emad
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor