I'm working on this website called codefights.com, and the task given in one of the coding challenges is described in this .  I'm trying to take add 1 to a specific position in a 2d array.  I use the following line to do so: newMatrix[i+1][j] += 1 but when I do that it, for some reason adds 1 to every array inside the array at that same position, not just at the one position I specified (Example below)  I would call the function with this array:   minesweeper([[true,false,false], [false,true,false], [false,false,false]])  def minesweeper(matrix):     rowlen = len(matrix[0])     columnLen = len(matrix)      newMatrix = [[0]*(rowlen+2)] * (columnLen+2) #grid of zeros with padding     truePosArray = []     y = 0     for x in matrix: #calculate the positions of the true instance(s)         try:             truePosArray.append([y+1, x.index(True)+1])         except ValueError:             pass         y += 1     if truePosArray == []:         return [[0]*(rowlen)] * (columnLen)      for y in truePosArray: #run once for every true instance         i = y[0]         j = y[1]         newMatrix[i+1][j] += 1         print(newMatrix)   When I print the first result of newMatrix during the last, the output is: [[0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0]]  Note: there are a padding of zeros around the array.  Why do you guys think this is happening, when I try to perform a similar action like this:  newMatrix = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]  i = 0 j = 1  newMatrix[i][j] += 1 print(newMatrix)   The output is [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]  Can anyone help me understand why this is happening? It has been confusing me for such a long time.   Thanks for your help!
410
I needed to create a list of lists in Python, so I typed the following: my_list = [[1] * 4] * 3  The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]    Then I changed one of the innermost values: my_list[0][0] = 5  Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]    which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it?
66754
I have been working on this exercise for a while now. It's in B.L. van der Waerden's Algebra (Volume I), page $19$. The exercise is as follows:     The order of the symmetric group $S_n$ is $n!=\prod_{1}^{n}\nu$. (Mathematical induction on $n$.)   I don't comprehend how we can logically use induction here. It seems that the first step would be proving $S_1$ has $1!=1$ elements. This is simply justified: There is only one permutation of $1$, the permutation of $1$ to itself.  The next step would be assuming that $S_n$ has order $n!$. Now here is where I get stuck. How do I use this to show that $S_{n+1}$ has order $(n+1)!$?  Here is my attempt: I am thinking this is because all $n!$ permutations of $S_n$ now have a new element to permutate. For example, if we take one single permutation $$ p(1,\dots,n) = \begin{pmatrix} 1 &amp; 2 &amp; 3 &amp; \dots &amp; n\\ 1 &amp; 2 &amp; 3 &amp; \dots &amp; n \end{pmatrix} $$ We now have $n$ modifications of this single permutation by adding the symbol $(n+1)$:  \begin{align} p(1,2,\dots,n,(n+1))&amp;= \begin{pmatrix} 1 &amp; 2 &amp; \dots &amp; n &amp; (n+1)\\ 1 &amp; 2 &amp; \dots &amp; n &amp; (n+1) \end{pmatrix}\\ p(2,1,\dots,n,(n+1))&amp;= \begin{pmatrix} 1 &amp; 2 &amp; \dots &amp; n &amp; (n+1)\\ 2 &amp; 1 &amp; \dots &amp; n &amp; (n+1) \end{pmatrix}\\ \vdots\\ p(n,2,\dots,1,(n+1))&amp;= \begin{pmatrix} 1 &amp; 2 &amp; \dots &amp; n &amp; (n+1)\\ n &amp; 2 &amp; \dots &amp; 1 &amp; (n+1) \end{pmatrix}\\ p((n+1),2,\dots,n,1)&amp;= \begin{pmatrix} 1 &amp; 2 &amp; \dots &amp; n &amp; (n+1)\\ (n+1) &amp; 2 &amp; \dots &amp; n &amp; 1 \end{pmatrix} \end{align}  There are actually $(n+1)$ permutations of that specific form, but we take $p(1,\dots,n)=p(1,\dots,n,(n+1))$ in order to illustrate and prove our original statement. We can make this general equality for all $n!$ permutations: $p(x_1,x_2,\dots,x_n)=p(x_1,x_2,\dots,x_n,x_{n+1})$ where $x_i$ is any symbol of our finite set of $n$ symbols and $x_{n+1}$ is strictly defined as the symbol $(n+1)$.  We can repeat this process for all $n!$ permutations in $S_n$. This gives us $n!n$ permutations. Then, adding in the original $n!$ permutations, we have $n!n+n!=(n+1)n!=(n+1)!$. Consequently, $S_n$ has order $n!$.  How is my reasoning here? Furthermore, is there a more elegant argument? I do not really see my argument here as incorrect, it just seems to lack elegance. My reasoning may well be very incorrect, however. If so, please point it out to me.
101020
I would like to sort a matrix according to a particular column. There is a sort function, but it sorts all columns independently.  For example, if my matrix data is:   1     3  5     7 -1     4   Then the desired output (sorting by the first column) would be:  -1     4  1     3  5     7   But the output of sort(data) is:  -1     3  1     4  5     7   How can I sort this matrix by the first column?
123656
We are given ($a_1,a_2,....,a_m$) and ($b_1, b_2,....,b_n$) sequences with non-negative integers.   Decide whether it's possible and if it is construct a matrix $\Re^{m x n}$ of ones("1") and zeros("0") where the number of ones("1") in row $i$ is $\forall i $ : $a_i$ and in column $j$ is $\forall j$ : $b_j$   Any hint is greatly appreciated.
77118
I will use binary.  I claim to have a bijection $f \colon \mathcal{N} \to \left[ 0, 1 \right)$ where $\mathcal{N}$ is the set of natural numbers $\left\{ 0, 1, 10, 11, 100, \dotsc \right\}$ as follows:  $$ \begin{array}{l|l} x \in \mathcal{N} &amp; f(x) \in \left[ 0, 1 \right)\\\hline 0 &amp; 0.0 \\ 1 &amp; 0.1 \\ 10 &amp; 0.01 \\ 11 &amp; 0.11 \\ 100 &amp; 0.001 \\ 101 &amp; 0.011 \\ 110 &amp; 0.101 \\ 111 &amp; 0.111 \\ 1000 &amp; 0.0001 \\ \hspace{.6em}\vdots &amp; \hspace{.9em}\vdots \end{array} $$  It's basically the .  I claim to have everything on this list.  Diagonalization?  Go ahead!  Constructing a number that differs from $f(i + 1)$ in its $i$-th fractional digit yields $0.11111\!\ldots = 0.\overline{1}$ which is $1$, and that should not be on the list anyway!  Why does this not work? I think it has something to do with integers supposedly not having infinite digits, while reals supposedly do.
25863
Is there a library function that performs binary search on a list/tuple and return the position of the item if found and 'False' (-1, None, etc.) if not?  I found the functions bisect_left/right in the , but they still return a position even if the item is not in the list. That's perfectly fine for their intended usage, but I just want to know if an item is in the list or not (don't want to insert anything).  I thought of using bisect_left and then checking if the item at that position is equal to what I'm searching, but that seems cumbersome (and I also need to do bounds checking if the number can be larger than the largest number in my list). If there is a nicer method I'd like to know about it.  Edit To clarify what I need this for: I'm aware that a dictionary would be very well suited for this, but I'm trying to keep the memory consumption as low as possible. My intended usage would be a sort of double-way look-up table. I have in the table a list of values and I need to be able to access the values based on their index. And also I want to be able to find the index of a particular value or None if the value is not in the list.  Using a dictionary for this would be the fastest way, but would (approximately) double the memory requirements.  I was asking this question thinking that I may have overlooked something in the Python libraries. It seems I'll have to write my own code, as Moe suggested.
1022
The following code works as expected in both Python 2.5 and 3.0:  a, b, c = (1, 2, 3)  print(a, b, c)  def test():     print(a)     print(b)     print(c)    # (A)     #c+=1       # (B) test()   However, when I uncomment line (B), I get an UnboundLocalError: 'c' not assigned at line (A). The values of a and b are printed correctly. This has me completely baffled for two reasons:   Why is there a runtime error thrown at line (A) because of a later statement on line (B)? Why are variables a and b printed as expected, while c raises an error?   The only explanation I can come up with is that a local variable c is created by the assignment c+=1, which takes precedent over the "global" variable c even before the local variable is created. Of course, it doesn't make sense for a variable to "steal" scope before it exists.  Could someone please explain this behavior?
19899
I don't understand why the determinant of the 0x0 matrix equals 1.
13411
Trivial question: Is there any standard notation for the concatenation of two or more matrices?  Example:  $$A = \left(\begin{array}[c c]  - a_1 &amp; a_2\\ a_3 &amp; a_4  \end{array}\right),$$  $$B = \left(\begin{array}[c c]  - b_1 &amp; b_2\\ b_3 &amp; b_4  \end{array}\right),$$  Then the concatenation (by rows) of $A$ and $B$ is:  $$C = \left(\begin{array}[c c]  - a_1 &amp; a_2\\ a_3 &amp; a_4 \\  b_1 &amp; b_2\\ b_3 &amp; b_4  \end{array}\right).$$  I just want to know if there is a standard notation for this operation.
114154
From my experience with modern programming and scripting languages, I believe most programmers are generally accustomed to referring to the first element of an array by 0 as index. Are there any substantial advantages in using 1?  I'm sure I've heard of more languages other than Zsh behaving similarly with arrays; it's fine by me, as it is equally convenient. However, as previously released and widely used shell scripting languages such as ksh and bash all use 0,  why would someone choose to alter this common "standard"?  My immediate answer to my question would be "of course not"; then, the only explanation I can think of regarding this somewhat "exclusive feature" to shells would be "they just did this to show off a bit more their cool shell".  I don't know much of either Zsh or its history though, and there is a high chance my trivial theory about this does not make any sense.  Is there an explanation for this?  Or is it just out of personal taste?
45957
I am creating a block-diagonal matrix and want to use boxes along the diagonal but the boxes don't touch each other, so it looks funny.  What's a workaround for this?  Here's my code:  \documentclass{article} \usepackage{amsmath} \begin{document} \[   \begin{bmatrix}            \fbox{$B_1$} &amp; 0 &amp; 0  \\      0 &amp; \fbox{$B_2$} &amp; 0 \\      0 &amp; 0 &amp; \fbox{$B_3$} \\    \end{bmatrix} \] \end{document}   something like the matrix in the middle ...of this answer (but without the red and color filling)    Thanks,
64500
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:  class PairEvaluator(HandEvaluator):   def returnArbitrary(self):     return ('ace', 'king')  pe = PairEvaluator() cards = pe.returnArbitrary() print('Two pair, {0}s and {1}s'.format(cards))   When I try to run this code, the compiler gives an IndexError: tuple index out of range. How should I structure my return value to pass it as an argument to .format()?
58547
I plan on building a game where enemies are randomly generated throughout the game. I plan on using an empty to run a Python script where it will add a predefined object at a given position (using random.randint to place them randomly around the map).  Is there a specific Python command where I can say something like "Create this object at this given x,y,z position"?  If so, can you please tell me what it is and how to use it?  Also, what is the difference between a Module and a Script in the Python controller?  Thanks so much for your help!  Alternatively, is there a Python function that sets an object's X,Y,Z coordinates?  If so, the empty can jump around randomly dropping enemies off at it's position.
45967
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off the bat with generating a deck of cards.  #include &lt;iostream&gt; #include &lt;vector&gt;  using namespace std;  int main() {     vector&lt;char&gt; deck;     char suit[] = {'h','d','c','s'};     char card[] = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'};     for (int j=0; j&lt;13; j++) {         for (int i=0; i&lt;4; i++) {             deck.push_back(card[j] suit[i]);         }            }      return 0; }   I know my problem begins with me trying to assign the value '10' to a char. Obviously I couldn't get this to compile but I'm sure when I try to assign the card values to the vector deck I'll also get an error since I used variable type 'char'. Knowing what kind of variable type to use seems to be killing me. Also, would 'deck.push_back(card[j] suit[i]);' be the correct code to combine the card and suit, or do you have to put something between card[j] and suit[i]? I'd appreciate it if any of you could lead me in the right direction. Also as a little side note, this is part of a homework assignment so please don't just give me entire blocks of code. Thanks for your help.
89126
I need to create a loop to answer part c of the following linear algebra question. I'm new to Matlab so this is extremely confusing. I'm not sure how to make a loop that "counts" from 0-25 in order to generate a new matrix for each value. If someone could help me out it'd be great.
15363
If you are creating a 1d array in Python is there any benefit to using the NumPy package?
99159
I didn't understand the permutation and of course, I got this question wrong.     Compute the inverse of the following permutation:   $$ \begin{pmatrix}  1&amp;2&amp;3&amp;4&amp;5&amp;6\\ 2&amp;5&amp;4&amp;1&amp;6&amp;3 \end{pmatrix} $$   Could you please help me with this question and provide an explanation?  Furthermore, is it possible to compute the direct permutation, not the inverse one? If yes, how?  Thank you in advance!