Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11164 > unrolled thread
| Started by | Jim <jianbao.tao@gmail.com> |
|---|---|
| First post | 2011-08-10 18:24 -0700 |
| Last post | 2011-08-10 21:29 -0700 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
Bizarre behavior of the 'find' method of strings Jim <jianbao.tao@gmail.com> - 2011-08-10 18:24 -0700
Re: Bizarre behavior of the 'find' method of strings MRAB <python@mrabarnett.plus.com> - 2011-08-11 02:48 +0100
Re: Bizarre behavior of the 'find' method of strings Chris Angelico <rosuav@gmail.com> - 2011-08-11 03:14 +0100
Re: Bizarre behavior of the 'find' method of strings Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-11 12:56 +1000
Re: Bizarre behavior of the 'find' method of strings Chris Rebert <clp2@rebertia.com> - 2011-08-10 20:49 -0700
Re: Bizarre behavior of the 'find' method of strings Jim <jianbao.tao@gmail.com> - 2011-08-10 21:29 -0700
Re: Bizarre behavior of the 'find' method of strings Jim <jianbao.tao@gmail.com> - 2011-08-10 21:29 -0700
| From | Jim <jianbao.tao@gmail.com> |
|---|---|
| Date | 2011-08-10 18:24 -0700 |
| Subject | Bizarre behavior of the 'find' method of strings |
| Message-ID | <2e5f052a-4922-433d-bbea-ec0c13e08a42@glegroupsg2000goo.googlegroups.com> |
Greetings, folks,
I am using python 2.7.2. Here is something I got:
>>> a = 'popular'
>>> i = a.find('o')
>>> j = a.find('a')
>>> a[i:j]
'opul'
Well, I expected a[i:j] to be 'opula', and can't think of any reason why this is not happening. So, can anybody help me out about this? Thank you very much.
Jim
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-08-11 02:48 +0100 |
| Message-ID | <mailman.2137.1313027314.1164.python-list@python.org> |
| In reply to | #11164 |
On 11/08/2011 02:24, Jim wrote:
> Greetings, folks,
>
> I am using python 2.7.2. Here is something I got:
>>>> a = 'popular'
>>>> i = a.find('o')
>>>> j = a.find('a')
>>>> a[i:j]
> 'opul'
>
> Well, I expected a[i:j] to be 'opula', and can't think of any reason
> why this is not happening. So, can anybody help me out about this?
> Thank you very much.
>
Python uses half-open ranges, which means that the start position is
inclusive and the end position is exclusive.
This means that a[i:j] returns the string starting at position i and
extending upto, but excluding, position j.
The reason that Python uses half-open ranges is that experience (with
the language Mesa) has shown that it results in fewer programming
errors that the alternatives.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-08-11 03:14 +0100 |
| Message-ID | <mailman.2138.1313028873.1164.python-list@python.org> |
| In reply to | #11164 |
On Thu, Aug 11, 2011 at 2:48 AM, MRAB <python@mrabarnett.plus.com> wrote: > Python uses half-open ranges, which means that the start position is > inclusive and the end position is exclusive. > Or if you prefer: Python identifies positions between characters, rather than characters. And I agree that it's better than the alternative - in fact, I wish other systems could work the same way (eg Bible references). If you think about position 0 being the very beginning of the string, and position len(s) being the very end, then you slice the string from position to position and take the characters between. When you index the string for a particular character, you aim at a position and take the character after it. Chris Angelico
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-11 12:56 +1000 |
| Message-ID | <4e4344ef$0$29982$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #11164 |
On Thu, 11 Aug 2011 11:24 am Jim wrote:
> Greetings, folks,
>
> I am using python 2.7.2. Here is something I got:
>>>> a = 'popular'
>>>> i = a.find('o')
>>>> j = a.find('a')
>>>> a[i:j]
> 'opul'
>
> Well, I expected a[i:j] to be 'opula', and can't think of any reason why
> this is not happening. So, can anybody help me out about this? Thank you
> very much.
Your subject line says:
Bizarre behavior of the 'find' method of strings
What makes you think this is a problem with the find method?
As a programmer, you should be able to isolate where the problem *actually*
occurs:
>>> a = 'popular'
>>> a.find('o')
1
>>> a.find('a')
5
So there is no problem with the find method: it correctly finds the index
(starting at zero, not one) of the first (going left-to-right) matching
substring.
Now, if you take a slice, you get an unexpected (to you) result:
>>> a[1:5]
'opul'
It doesn't matter where the 1 and 5 come from. The slice can't tell that
they were the output of find, or how they were generated:
>>> a[1000-999:20/4]
'opul'
Now that you have isolated the actual problem, you can ask a better
question:
"Why does slicing not work the way I expect?"
Answer: because Python uses half-open slices, where the end parameter is not
included. The reason for that is that experience with other languages shows
that it leads to fewer "off-by-one" errors.
See also:
http://mail.python.org/pipermail/tutor/2010-December/080592.html
http://en.wikipedia.org/wiki/Off-by-one_error
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-08-10 20:49 -0700 |
| Message-ID | <mailman.2145.1313034593.1164.python-list@python.org> |
| In reply to | #11171 |
On Wed, Aug 10, 2011 at 7:56 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Thu, 11 Aug 2011 11:24 am Jim wrote:
>
>> Greetings, folks,
>>
>> I am using python 2.7.2. Here is something I got:
>>>>> a = 'popular'
>>>>> i = a.find('o')
>>>>> j = a.find('a')
>>>>> a[i:j]
>> 'opul'
>>
>> Well, I expected a[i:j] to be 'opula', and can't think of any reason why
>> this is not happening. So, can anybody help me out about this? Thank you
>> very much.
<snip>
> "Why does slicing not work the way I expect?"
>
> Answer: because Python uses half-open slices, where the end parameter is not
> included. The reason for that is that experience with other languages shows
> that it leads to fewer "off-by-one" errors.
>
> See also:
>
> http://mail.python.org/pipermail/tutor/2010-December/080592.html
> http://en.wikipedia.org/wiki/Off-by-one_error
And further:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
Cheers,
Chris
[toc] | [prev] | [next] | [standalone]
| From | Jim <jianbao.tao@gmail.com> |
|---|---|
| Date | 2011-08-10 21:29 -0700 |
| Message-ID | <mailman.2148.1313036944.1164.python-list@python.org> |
| In reply to | #11177 |
Thanks for all you guys. Now I got it. To Steven, I was in a little rush when I put this post. But you are right. It's not the find method's problem.
[toc] | [prev] | [next] | [standalone]
| From | Jim <jianbao.tao@gmail.com> |
|---|---|
| Date | 2011-08-10 21:29 -0700 |
| Message-ID | <f7a579d9-9498-4b81-a030-d9d8a65ac183@glegroupsg2000goo.googlegroups.com> |
| In reply to | #11177 |
Thanks for all you guys. Now I got it. To Steven, I was in a little rush when I put this post. But you are right. It's not the find method's problem.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web