Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Matrix: Value not in list error
    primarykey
    data
    text
    <p>I am writing a Python program that creates a 9x9 matrix with all of the values being 0. Then I have to manually put in the a actual values I want to be in it. (That is why I have all of the inserts.) I am trying to use the list.remove(x) command with list being the matrix and x being the value I am trying to remove. I know that the x value I am putting in is in the matrix but I keep getting an error saying it isn't.</p> <p>Here is my code:</p> <pre><code>matrix = [[0 for x in range (9)] for y in range (9)] C = matrix.count([0, 0, 0, 0, 0, 0, 0, 0, 0]) matrix.insert(0, [0, 0, 0, 5, 0, 0, 0, 0, 6]) matrix.insert(0, [8, 0, 0, 0, 4, 7, 5, 0, 3]) matrix.insert(0, [0, 5, 0, 0, 0, 3, 0, 0, 0]) matrix.insert(0, [0, 7, 0, 8, 0, 0, 0, 0, 9]) matrix.insert(0, [0, 0, 0, 0, 1, 0, 0, 0, 0]) matrix.insert(0, [9, 0, 0, 0, 0, 4, 0, 2, 0]) matrix.insert(0, [0, 0, 0, 9, 0, 0, 0, 1, 0]) matrix.insert(0, [7, 0, 8, 3, 2, 0, 0, 0, 5]) matrix.insert(0, [3, 0, 0, 0, 0, 8, 0, 0, 0]) matrix.reverse() for sublist in matrix: s = str(sublist) print (s) print (C) matrix.remove("[0, 0, 0, 0, 0, 0, 0, 0, 0]") </code></pre> <p>Here is the error I keep getting:</p> <pre><code>Traceback (most recent call last): line 17, in &lt;module&gt; matrix.remove("[0, 0, 0, 0, 0, 0, 0, 0, 0]") ValueError: list.remove(x): x not in list </code></pre> <p>I also tried using this code but Python seems to just find that it isn't in the matrix anyways. This I put in place of matrix.remove("[0, 0, 0, 0, 0, 0, 0, 0, 0]") Here it is:</p> <pre><code>if "[0, 0, 0, 0, 0, 0, 0, 0, 0]" in matrix: matrix.remove("[0, 0, 0, 0, 0, 0, 0, 0, 0]") </code></pre> <p>I am very new to Python and am still learning. Any help you might give me would be greatly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload