s.index(x[, i[, j]]) will change the s ?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Thu Sep 10 00:30:35 EDT 2009
On Wed, 09 Sep 2009 21:00:20 -0700, s7v7nislands wrote:
> 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:
>>> 'abcd'.index('c')
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().
--
Steven
More information about the Python-list
mailing list