ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 22 Mar 2021 12:15:36 +0100Bad index in a sublisthttps://ask.sagemath.org/question/56296/bad-index-in-a-sublist/ In the following code
A=[[99999,99999,1,99999],[1,99999,1,99999]]
A[0].index(A[0][2])
A[0].index(A[0][0])
A[0].index(A[0][1])
The index returned is the true one if the value is encountered form the first time in the 0th element of `A`. So `A[0].index(A[0][2])` and `A[0].index(A[0][0])` give the good index. But not `A[0].index(A[0][1])`. Why ?Sun, 21 Mar 2021 11:27:15 +0100https://ask.sagemath.org/question/56296/bad-index-in-a-sublist/Answer by tmonteil for <p>In the following code</p>
<pre><code>A=[[99999,99999,1,99999],[1,99999,1,99999]]
A[0].index(A[0][2])
A[0].index(A[0][0])
A[0].index(A[0][1])
</code></pre>
<p>The index returned is the true one if the value is encountered form the first time in the 0th element of <code>A</code>. So <code>A[0].index(A[0][2])</code> and <code>A[0].index(A[0][0])</code> give the good index. But not <code>A[0].index(A[0][1])</code>. Why ?</p>
https://ask.sagemath.org/question/56296/bad-index-in-a-sublist/?answer=56297#post-id-56297First, your question adds a useless level of complexity,
since everything happens within `A[0]`, so let me first reduce it:
sage: B = [9, 9, 1, 9]
sage: B.index(B[0])
0
sage: B.index(B[1])
0
sage: B.index(B[2])
2
sage: B.index(B[3])
0
You can check the documentation of the `index` method of lists as follows:
sage: B.index?
As you can see, this method B.index(x) returns the smallest
index `i` such that B[i]=x (when it exists, otherwise it raises a `ValueError`).
In our case, there are three indices `i` for which `B[i]` equals `9`, namely `0`, `1`, `3`.
The smallest of them is `0`, hence `B.index(9)` will return `0`.
The fact that `9` was provided by `B[0]`, `B[1]` or `B[3]` does not matter.
Note that this question is only about the Python language, not Sage.
Hence you might find tons of explanations about this on the web.Sun, 21 Mar 2021 11:47:51 +0100https://ask.sagemath.org/question/56296/bad-index-in-a-sublist/?answer=56297#post-id-56297Comment by Cyrille for <p>First, your question adds a useless level of complexity,
since everything happens within <code>A[0]</code>, so let me first reduce it:</p>
<pre><code>sage: B = [9, 9, 1, 9]
sage: B.index(B[0])
0
sage: B.index(B[1])
0
sage: B.index(B[2])
2
sage: B.index(B[3])
0
</code></pre>
<p>You can check the documentation of the <code>index</code> method of lists as follows:</p>
<pre><code>sage: B.index?
</code></pre>
<p>As you can see, this method B.index(x) returns the smallest
index <code>i</code> such that B[i]=x (when it exists, otherwise it raises a <code>ValueError</code>).</p>
<p>In our case, there are three indices <code>i</code> for which <code>B[i]</code> equals <code>9</code>, namely <code>0</code>, <code>1</code>, <code>3</code>.
The smallest of them is <code>0</code>, hence <code>B.index(9)</code> will return <code>0</code>.
The fact that <code>9</code> was provided by <code>B[0]</code>, <code>B[1]</code> or <code>B[3]</code> does not matter.</p>
<p>Note that this question is only about the Python language, not Sage.
Hence you might find tons of explanations about this on the web.</p>
https://ask.sagemath.org/question/56296/bad-index-in-a-sublist/?comment=56310#post-id-56310Sorry to both. As I am not a professional in programming some times, I lose control.Mon, 22 Mar 2021 12:15:36 +0100https://ask.sagemath.org/question/56296/bad-index-in-a-sublist/?comment=56310#post-id-56310