Ask your own question, for FREE!
MIT 6.01SC Introduction to Electrical Engineering and Computer Science I 9 Online
OpenStudy (anonymous):

There is this issue about lists in python I have encountered which is probably about the code I wrote so some insight will be appreciated. Problem Wk.6.1.3: A bigger bunch of zeros and code goes wrong is : def zeroVector(n): a=list(range(n)) b=[] k=0 while k

OpenStudy (anonymous):

Are you trying to generate a two dimensional list of zeros? I'm not sure I understand your purpose, but if so there are a few ways to do it. For a list: def zeroArray(m, n): return [[0]*n]*m Alternatively, if you want a numpy array: import numpy as np def zeroArray(m,n): return np.zeros(m*n).reshape(m,n)

OpenStudy (anonymous):

Also, I see what you did wrong in your code, if you do want to do it that way. You say: foo=zeroVector(m) print(foo) for f in foo: foo[f]=zeroVector(n) print(foo) However, look at what foo is. It is zeroVector(m), which outputs a vector full of zeros that is of length m. So you are looping over values of zero m times. Essentially you are doing foo[0]=[0,0....,0] a lot of times. Your for statement should be 'for f in range(len(foo)):'

OpenStudy (anonymous):

Thank you a lot.Apart from the first statement does it nicely, now I know a bit more about what numpy does that I didnt bother when it refused to install (have both python 2.7 and 3.3).However from my ambigious question this remains still; if foo is a list and "for ... in" statement iterates on any list's elements why should use range(len(foo)). And once again you've earned best credits for your great help already

OpenStudy (anonymous):

Imagine a list ">>> alphabet = ['a', 'b', 'c']" When you do for x in alphabet: print x What you are doing is iterating over the elements in alphabet. So the output would be: 'a' 'b' 'c' So in the original statement, you say ">>>for f in foo:". But foo is a list of zeros. So you are repeatedly giving f a value of zero. You then loop through foo[f]. So what are you doing? You are assigning things to the 0th index of foo and then assigning something else to foo[0]. You are saying foo[0] = [0,0,0...,0] a lot of times in a row. range(n) creates a list object that goes from 0 -> n-1. [0, 1, 2, 3, ..., n-1]. You are trying to access the f index of foo. Now you are saying foo[0] = [0,0,0...,0] foo[1] = [0,0,0...,0] ... foo[n-1] = [0,0,0...,0] Which is what you were trying to do.

OpenStudy (anonymous):

So if I understand correctly iteration element f (f in foo) has the value of 0 (more elaborately any item's value by an index number from 0 to top which is not shown).I got it now.I began to dig through some other lessons as well and YOU have been a great help. Yet when everything looked settled a new issue arose;if we use the code below def zeroArray(m, n): return [[0]*n]*m and try to assign 2nd element of the 2nd element of constructed list say like: z=zeroArray(3,4) by z[2][2]=1 value of z becomes; [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]] you are already a charm even if you cant tell me if any way exists to make such assignment

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!