s.index(x[, i[, j]]) will change the s ?

S

s7v7nislands

hi all:
what is the s.index() mean? does the index() change the s?
In python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:

Raises ValueError when x is not found in s. When a negative index is
passed as the second or third parameter to the index() method, the
list length is added, as for slice indices. If it is still negative,
it is truncated to zero, as for slice indices.

Changed in version 2.3: Previously, index() didn’t have arguments for
specifying start and stop positions.

who can give a example? and why the s.remove() also point to note 4?
Is the document wrong?
 
R

r

hi all:
    what is the s.index() mean? does the index() change the s?
    In python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:

Raises ValueError when x is not found in s. When a negative index is
passed as the second or third parameter to the index() method, the
list length is added, as for slice indices. If it is still negative,
it is truncated to zero, as for slice indices.

Changed in version 2.3: Previously, index() didn’t have arguments for
specifying start and stop positions.

who can give a example?  and why the s.remove() also point to note 4?
Is the document wrong?

the Python command line is your buddy and the IDLE shell is your BFF.
'abcdefg'

Python strings are immutable!
http://en.wikipedia.org/wiki/Immutable_object

try this command
 
S

Steven D'Aprano

hi all:
what is the s.index() mean? does the index() change the s?

It returns the index (position) of its argument in the object s. Have you
tried it to see for yourself?
['a', 'b', 'c', 'd'].index('c') # 'c' is in position 2
2
['a', 'b', 'c', 'd'].index('e') # 'e' is missing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list



It works the same for strings:
2

For strings, it is just like find() except it raises an exception instead
of returning -1. What makes you think that this changes s?


In
python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:

Raises ValueError when x is not found in s. When a negative index is
passed as the second or third parameter to the index() method, the list
length is added, as for slice indices. If it is still negative, it is
truncated to zero, as for slice indices.

Changed in version 2.3: Previously, index() didn’t have arguments for
specifying start and stop positions.

who can give a example? and why the s.remove() also point to note 4? Is
the document wrong?


s.remove() points to note 4 because everything in note 4 applies to
s.remove() as well as s.index().
 
C

Chris Rebert

hi all:
   what is the s.index() mean? does the index() change the s?

It tells you the index of the first instance of the given element in
the sequence. Or, to quote the docs:
s.index(x[, i[, j]]) --- return smallest k such that s[k] == x and
i <= k < j

No, .index() does not modify the sequence itself.
   In python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:

Raises ValueError when x is not found in s. When a negative index is
passed as the second or third parameter to the index() method, the
list length is added, as for slice indices. If it is still negative,
it is truncated to zero, as for slice indices.

Changed in version 2.3: Previously, index() didn’t have arguments for
specifying start and stop positions.

Nothing in the above says anything about modifying a sequence...
who can give a example?  and why the s.remove() also point to note 4?

Because it has the same behavior when the item is not present in the sequence.

Examples using lists:

assert ["c", "a", "b", "c", "c"].index("c", 1) == 3

try:
["a", "b"].index("c")
except ValueError:
print "'c' was not in the list"
else:
raise RuntimeError, "Should never get here"

x = ["a", "b", "c"]
x.remove("b")
assert len(x) == 2 and x[0] == "a" and x[1] == "c"
Is the document wrong?

No. What made you think so?

Cheers,
Chris
 
S

s7v7nislands

Thanks for your reply! Sorry for my poor english!

hi all:
   what is the s.index() mean? does the index() change the s?

It tells you the index of the first instance of the given element in
the sequence. Or, to quote the docs:
    s.index(x[, i[, j]]) --- return smallest k such that s[k] == x and
i <= k < j

No, .index() does not modify the sequence itself.

I known index() does not modify the sequence itself. my question is so
why the doc put the index() method in the mutable sequence types list?
Nothing in the above says anything about modifying a sequence...
When a negative index is passed as the second or third parameter to
the index() method, the list length is added, as for slice indices.
I don't understand the mean. the list length is added, why? if it
changed, the original will change ?
who can give a example?  and why the s.remove() also point to note 4?

Because it has the same behavior when the item is not present in the sequence.

Examples using lists:

assert ["c", "a", "b", "c", "c"].index("c", 1) == 3

try:
    ["a", "b"].index("c")
except ValueError:
    print "'c' was not in the list"
else:
    raise RuntimeError, "Should never get here"

x = ["a", "b", "c"]
x.remove("b")
assert len(x) == 2 and x[0] == "a" and x[1] == "c"
I want a example, maybe: use the a negative index is passed as the
second or third parameter, and see the length changed.
No. What made you think so?
Sorry for my poor english. do you understand me now? thanks!
 
C

Chris Rebert

I known index() does not modify the sequence itself. my question is so
why the doc put the index() method in the mutable sequence types list?

Ah, okay. Now I understand your question. I don't know really. I
suppose it could just as well go under the more general table for
"6.6. Sequence Types". I'd guess it's under Mutable Sequence Types for
convenience's sake since .remove() is mutable-only and makes reference
to the same footnote. You could file a documentation bug:
http://bugs.python.org

When a negative index is passed as the second or third parameter to
the index() method, the list length is added, as for slice indices.
I don't understand the mean.  the list length is added, why? if it
changed, the original will change ?

This feature just lets you use negative list indices, just like with
slicing. For example:

x = ["a", "b", "c", "d"]
assert x[-1] == x[len(x) - 1] == x[3] == "d"
assert x[-2] == x[len(x) - 2] == x[2] == "c"
assert x[-3] == x[len(x) - 3] == x[1] == "b"
assert x[-4] == x[len(x) - 4] == x[0] == "a"

See also tec's examples.

Cheers,
Chris
 
S

Steven D'Aprano

When a negative index is passed as the second or third parameter to the
index() method, the list length is added, as for slice indices. I don't
understand the mean. the list length is added, why? if it changed, the
original will change ? ....
I want a example, maybe: use the a negative index is passed as the
second or third parameter, and see the length changed.


Passing a negative parameter doesn't change the list. Why don't you try
it for yourself and see?
alist = 'a b c a b c a b'.split()
alist ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b']
alist.index('a') # start at the beginning 0
alist.index('a', 1) # start 1 from the beginning 3
alist.index('a', 5) # start 5 from the beginning 6
alist.index('a', 8-3) # start 3 from the end 6
alist.index('a', -3) # start 3 from the end 6
alist # alist is unchanged
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b']
 
T

Terry Reedy

Because once upon a time, only lists and not tuples had the method.
Ah, okay. Now I understand your question. I don't know really. I
suppose it could just as well go under the more general table for
"6.6. Sequence Types". I'd guess it's under Mutable Sequence Types for
convenience's sake since .remove() is mutable-only and makes reference
to the same footnote. You could file a documentation bug:
http://bugs.python.org

I already did.
http://bugs.python.org/issue4966

Producing an edited version of the doc text from my suggestions is on my
todo list if no one beats me to it. I need to learn .rst better, though.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top