A community for students.
Here's the question you clicked on:
 0 viewing
 2 years ago
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<n:
b.append(0)
k+=1
for e in a:
a[e]=0
print(a)
return a
def zeroArray(m,n):
foo=zeroVector(m)
print(foo)
for f in foo:
foo[f]=zeroVector(n)
print(foo)
return foo
which has this return for zeroArray(3,4):
[[0, 0, 0, 0], 0, 0]
 2 years ago
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<n: b.append(0) k+=1 for e in a: a[e]=0 print(a) return a def zeroArray(m,n): foo=zeroVector(m) print(foo) for f in foo: foo[f]=zeroVector(n) print(foo) return foo which has this return for zeroArray(3,4): [[0, 0, 0, 0], 0, 0]

This Question is Open

rapidsnow
 2 years ago
Best ResponseYou've already chosen the best response.1Are 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)

rapidsnow
 2 years ago
Best ResponseYou've already chosen the best response.1Also, 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)):'

Sarper
 2 years ago
Best ResponseYou've already chosen the best response.0Thank 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

rapidsnow
 2 years ago
Best ResponseYou've already chosen the best response.1Imagine 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 > n1. [0, 1, 2, 3, ..., n1]. 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[n1] = [0,0,0...,0] Which is what you were trying to do.

Sarper
 2 years ago
Best ResponseYou've already chosen the best response.0So 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.