1

I have an array of arrays filled with zeros, so this is the shape I want for the result.

I'm having trouble saving the nested for-loop to this array of arrays. In other words, I want to replace all of the zeros with what the last line calculates.

percent = []
for i in range(len(F300)):
    percent.append(np.zeros(lengths[i]))

for i in range(0,len(Name)):
    for j in range(0,lengths[i]):
        percent[i][j]=(j+1)/lengths[i]

The last line only saves the last j value for each i.

I'm getting:

percent = [[0,0,1],[0,1],[0,0,0,1]]

but I want:

percent = [[.3,.6,1],[.5,1],[.25,.5,75,1]]
5
  • 1
    Try : from __future__ import division at the start. Commented Oct 31, 2017 at 18:34
  • Just move to python 3 already Commented Oct 31, 2017 at 18:38
  • @percusse I can't speak for dontdeimos, but the Maya cluster I'm employed to develop software for still runs on Canopy Python 2, and the HPCF maintaining it has no intention of upgrading any time soon. Consequentially, users like myself have to design everything with cross-version compatibility in mind. Sometimes you have to work with what you're given. Commented Oct 31, 2017 at 19:19
  • @ErickShepherd In two years, that argument will hopefully go away. Commented Oct 31, 2017 at 20:01
  • @percusse Fingers crossed. It'll save me so much typing and a fair bit of head scratching when they finally do upgrade. Commented Oct 31, 2017 at 20:05

1 Answer 1

1

The problem with this code is that because it's in Python 2.7, the / operator is performing "classic" division. There are a couple different approaches to solve this in Python 2.7. One approach is to convert the numbers being divided into floating point numbers:

import numpy as np

lengths = [3, 2, 4] # Deduced values of lengths from your output.
percent = []

for i in range(3): # Deduced size of F300 from the length of percent.

    percent.append(np.zeros(lengths[i]))

for i in range(0, len(percent)):

    for j in range(0, lengths[i]): #

        percent[i][j] = float(j + 1) / float(lengths[i])

Another approach would be to import division from the __future__ package. However, this import line must be the first statement in your code.

from __future__ import division
import numpy as np

lengths = [3, 2, 4] # Deduced values of lengths from your output.
percent = []

for i in range(3): # Deduced size of F300 from the length of percent.

    percent.append(np.zeros(lengths[i]))

for i in range(0, len(percent)):

    for j in range(0, lengths[i]):

        percent[i][j] = (j + 1) / lengths[i]

The third approach, and the one I personally prefer, is to make good use of NumPy's built-in functions:

import numpy as np

lengths = [3, 2, 4] # Deduced values of lengths from your output.
percent = np.array([np.linspace(1.0 / float(l), 1.0, l) for l in lengths])

All three approaches will produce a list (or in the last case, numpy.ndarray object) of numpy.ndarray objects with the following values:

[[0.33333333, 0.66666667, 1.], [0.5, 1.], [0.25, 0.5, 0.75, 1.]]
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thank you so much. I can't believe it was such a small issue with python 2.7. Is the {/} different in python 3?
@dontdeimos Yes, in Python 3, the / operator returns the floating point quotient of the division of two numbers, whether they are of type int, type float, or some combination thereof. However, in Python 2, / performs "classic" division, which for int values is sort of the partner of the modulo operation (via the % operator): / gives how many times int denominator divides the int numerator, the quotient of which is also an int, and % gives the int remainder, very much analogous to quotients and remainders in long division from elementary school.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.