I have the following simple 2D lists program:  rowMatrix = [0]*4 matrix = [rowMatrix]*4  for a in range(4):     for b in range(4):         matrix[a][b] = random.randrange(0,2) print(matrix)`   It seems like it would put random numbers (0, 1) in all 16 cells of the 4x4 array. But all 4 rows are identical every time. What's wrong?
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?
27093
I occasionally see the list slice syntax used in Python code like this:  newList = oldList[:]   Surely this is just the same as:  newList = oldList   Or am I missing something?
74245
In the  book on pg 130, they mention that an Array's length is fixed when you initialize it and that multidimensional arrays are not supported. I just tried using the .add function on an array and it worked so I'm sure what they are talking about, but I couldn't make an array of arrays (which could have just been because I didn't have the right syntax). Ignoring syntax errors, there seems to be some sort of difference and thus maybe an (even if just slight) advantage to using one over the other?
108260
So I have been tasked to make an array without duplicated values from another existing array. So I did it, but I want to know is there some other better way to do that. Example input/output: Input: 10, 15, 10, 5, 1, 3 Output: 10, 15, 5, 1, 3 So here is my code. #include &lt;stdio.h&gt;  int main(void) {   const int MAX_ARRAY_SIZE = 5;    int m[MAX_ARRAY_SIZE], p[MAX_ARRAY_SIZE];     for(int i = 0; i &lt; MAX_ARRAY_SIZE; i++) {     printf(&quot;Enter number: &quot;);     scanf(&quot;%d&quot;,&amp;m[i]);   }   int k = 0;   int dup = 0;   for(int i =0; i &lt; MAX_ARRAY_SIZE; i++) {     for(int j = i +1; j &lt;MAX_ARRAY_SIZE; j++) {         if(m[i] == m[j]) {             dup = 1;         }     }     if(dup != 1) {       p[k++] = m[i];     }     dup = 0;   }    printf(&quot;The new array without repeated values\n&quot;);   for(int i = 0; i &lt; k; i++) {     printf(&quot;%d\n&quot;,p[i]);   }    return 0; }  I'm not sure if this is the right and simple way I do that. I want some suggestions. Thanks in advance. :)
34147
I'm trying to identify null/0 values in each of my raster bands. How can I loop through all the values of each band? I've tried this code, straight from the gdal/python cookbook website but this just gives stats for each band as a whole and for some reason it actually doesn't print a minimum value of 0 even though my raster does have 0 values, it shows a minimum value of 1.   I want to print every single cell value. Is there a way to do this?   from osgeo import gdal import sys  src_ds = gdal.Open( "INPUT.tif" )  print "[ RASTER BAND COUNT ]: ", src_ds.RasterCount for band in range( src_ds.RasterCount ):     band += 1     print "[ GETTING BAND ]: ", band     srcband = src_ds.GetRasterBand(band)      stats = srcband.GetStatistics( True, True )      print "[ STATS ] =  Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % ( \             stats[0], stats[1], stats[2], stats[3] )
53456
What is the difference between:  some_list1 = [] some_list1.append("something")   and  some_list2 = [] some_list2 += ["something"]
94938
In R, if I set.seed(), and then use the sample function to randomize a list, can I guarantee I won't generate the same permutation?  ie...  set.seed(25) limit &lt;- 3 myindex &lt;- seq(0,limit) for (x in seq(1,factorial(limit))) {     permutations &lt;- sample(myindex)     print(permutations) }   This produces  [1] 1 2 0 3 [1] 0 2 1 3 [1] 0 3 2 1 [1] 3 1 2 0 [1] 2 3 0 1 [1] 0 1 3 2   will all permutations printed be unique permutations?  Or is there some chance, based on the way this is implemented, that I could get some repeats?  I want to be able to do this without repeats, guaranteed.  How would I do that?  (I also want to avoid having to use a function like permn(), which has a very mechanistic method for generating all permutations---it doesn't look random.)  Also, sidenote---it looks like this problem is O((n!)!), if I'm not mistaken.
9180
First off, this question is ripped out from  question. I did it because I think this part is bigger than a sub-part of a longer question. If it offends, please pardon me.  Assume that you have a algorithm that generates randomness. Now how do you test it? Or to be more direct - Assume you have an algorithm that shuffles a deck of cards, how do you test that it's a perfectly random algorithm?  To add some theory to the problem - A deck of cards can be shuffled in 52! (52 factorial) different ways. Take a deck of cards, shuffle it by hand and write down the order of all cards. What is the probability that you would have gotten exactly that shuffle? Answer: 1 / 52!.  What is the chance that you, after shuffling, will get A, K, Q, J ... of each suit in a sequence? Answer 1 / 52!  So, just shuffling once and looking at the result will give you absolutely no information about your shuffling algorithms randomness. Twice and you have more information, Three even more...  How would you black box test a shuffling algorithm for randomness?
54606
I've never quite got this. Just say you write a small program in any language at all which rolls some dice (just using dice as an example). After 600,000 rolls, each number would have been rolled around 100,000 times, which is what I'd expect.  Why are there websites dedicated to 'true randomness'? Surely, given the observation above, the chances of getting any number are almost exactly 1 over how ever many numbers it can choose from.  I tried it in : Here's the result of 60 million rolls. The highest variation is like 0.15. Isn't that as random as it's going to get?  1 - 9997653 2347.0 2 - 9997789 2211.0 3 - 9996853 3147.0 4 - 10006533 -6533.0 5 - 10002774 -2774.0 6 - 9998398 1602.0
84534
So I want to find the number of ways to fill an $m\times n$ matrix with only 0s and 1s such that each row and column has an even number of 1s.  I'm pretty stumped here.  I've set up m+n equations summing the respective rows and columns yielding 0 mod2, but other than that I'm not too sure of a clean way to approach this.  The subsequent part asks for the same thing except that there's an odd number of 1s.  Any hints and/or tips very much welcome.
832
.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization.   Arrays are very easy to initialize with a default value, and by definition they already have certain size:  string[] Ar = new string[10];   Which allows one to safely assign random items, say:  Ar[5]="hello";   with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:  List&lt;string&gt; L = new List&lt;string&gt;(10); for (int i=0;i&lt;10;i++) L.Add(null);   or  string[] Ar = new string[10]; List&lt;string&gt; L = new List&lt;string&gt;(Ar);   What would be a cleaner way?  EDIT: The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do L[2]="somevalue"  EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:   One could quite convincingly argue that lists are the "next generation" arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I'm pointing out they might not be as easy to initialize. What I'm currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.
123306
What is the probability that a random $\{0,1\}$, $n \times n$ matrix is invertible? Assume the 0 and 1 are each present in an entry with probability $\frac{1}{2}$. Is there an explicit formula as a function of $n$?  Does it tend to 1 as $n$ grows large? I'm sure this is all known...  Thanks!
17923
I have an array in the form of 'int[][]' that represents the co-ordinates of a small grid. Each co-ordinate has been assigned its own value. eg array[0][4] = 28......  I have two questions. Firstly, how do I iterate through all the stored values. Secondly, I want to be able to input a value and have its specific co-ordinates in the grid returned. What would be the best way to approach this?  Thank you for any help!
1520
I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] and makes it into iterable list of tuples, like so: [(0,3), (1,7), (2,19)] to use it instead of:  for i in range(len(name_of_list)):     name_of_list[i] = something   but I can't remember the name and googling "iterate list" gets nothing.
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.
54582
Suppose I have a $n\times n$ matrix $A$. Can I, by using only pre- and post-multiplication by permutation matrices, permute all the elements of $A$? That is, there should be no binding conditions, like $a_{11}$ will always be to the left of $a_{n1}$, etc.  This seems to be intuitively obvious. What I think is that I can write the matrix as an $n^2$-dimensional vector, then I can permute all entries by multiplying by a suitable permutation matrix, and then re-form a matrix with the permuted vector.
34282
I have random variables $X_{1}, X_{2}, \dots X_{n}$ that are each Unif$\{1, \dots, n\}$, and I want to calculate $$P(\{X_{1}, X_{2}, \dots X_{n}\}=\mathbb{N})$$  My intuition is that this means each $X_{n} = n$, which occurs with probability $\frac{1}{n!}$.  Does this seem correct?