Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Sarper

  • 3 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
  1. rapidsnow
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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)

  2. rapidsnow
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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)):'

  3. Sarper
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  4. rapidsnow
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  5. Sarper
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy