anonymous
  • 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
MIT 6.01SC Introduction to Electrical Engineering and Computer Science I
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • 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)
anonymous
  • 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)):'
anonymous
  • 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

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • 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.
anonymous
  • 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

Looking for something else?

Not the answer you are looking for? Search for more explanations.