1

I get the error 'float' object is not subscriptable when I run the following code: I believe that the error occurs since I am inputting the values of surRates using their list positions.

survRates = [[],[],[]]
newList = []
totalGens = 3

survRates[0] = 1.5
survRates[1] = 3.5
survRates[2] = 2.5

def gens():
    for i in range(totalGens):
        dataSet = []
        for j in range(3):
            dataSet.append([])
        newList.append(dataSet)
    print(newList)

def adultCalc(juvs):    
    newAdults = juvs * survRates[0][0]
    return newAdults

def senCalc(adults,sens):
    newSens = round((adults* survRates[1][0]) + (sens* survRates[2][0]),2)
    return newSens

generations()
newList[0][0] = 3.5
newList[0][1] = adultCalc(3)
newList[0][2] = senCalc(6,3)

print(newList)

When I run the same code by changing the code below to survRates = [[1.5],[3.5],[2.5]] instead of initializing the variables, it works.

survRates[0] = 1.5
survRates[1] = 3.5
survRates[2] = 2.5

My desired outcome is: [[1.5, 3.5, 2.1], [[], [], []], [[], [], []]], which works if I use survRates = [[1.5],[3.5],[2.5]]

2 Answers 2

1

Is your desired result:

[[1.5, 3.5, 2.1], [[], [], []], [[], [], []]]?

or is it ?

[[[1.5], [3.5], [2.1]], [[], [], []], [[], [], []]]?

because the second one is an equal data structure more likely what you want.

Then you need to not do:

survRates[0] = 1.5
survRates[1] = 3.5
survRates[2] = 2.5

Instead do:

survRates[0] = []
survRates[0][0] = 1.5
survRates[0][1] = 3.5
survRates[0][2] = 2.5

or since you have very specific requirements: it looks like you had a typo in your question: this probably what you really wanted.

survRates = [[[], [], []], [[], [], []], [[], [], []]]
survRates[0][0][0] = 1.5
survRates[0][0][1] = 3.5
survRates[0][0][2] = 2.5

That way each array will be an array with just one element... for I'm sure nefarious purposes.

You were not accurately describing what you wanted:

You wanted to set the position 0 (first element) in a list but not the list of survRates but instead the list in survRates's first element's list (triply nested!). So for nested lists like survRates in order to access the first element of the first list you use: an_array[0][0][0] = just doing an_array[0] = or an_array[0][0] = will attempt to set the first element of survRates not survRates' first list or that list's first list.

This part generates: [[[], [], []], [[], [], []], [[], [], []]]

def gens():
    for i in range(totalGens):
        dataSet = []
        for j in range(3):
            dataSet.append([])
        newList.append(dataSet)
    print(newList)

If you want further help here explain what's going on a bit deeper.

newAdults = juvs * survRates[0][0]

Having a hardcoded survRates[0][0] will return a list not an element probably unlikely that you want to multiply juvs by a list with 3 mathematical elements probably you want survRates[0][0][0] there.

UPDATE ==> Since they're saying they do particularly want it to have 1 array in the first data list and 3 arrays in each subsequent datalist:

gens() function should be modified:

def gens():
    for i in range(totalGens):
        dataSet = []
        for j in range(3):
            if i > 0:
                dataSet.append([])
        newList.append(dataSet)
    print(newList)

now we will have =>

[[], [[], [], []], [[], [], []]]

This part in the beginning of your post will still give an error:

survRates[0] = 1.5
survRates[1] = 3.5
survRates[2] = 2.5

But will work is:

 survRates[0][0] = 1.5
 survRates[0][1] = 3.5
 survRates[0][2] = 2.5
Sign up to request clarification or add additional context in comments.

3 Comments

I would like to input the values 1.5,3.5 and 2.1 into the survRates like so [[1.5, 3.5, 2.1], [[], [], []], [[], [], []]]. Here is a link to my previous question with the chosen answer. I think both of these questions are linked.
but why such an uneven data structure? sometimes you want a list with 3 elements and other times an list of 3 lists internally? are you sure that maybe a dictionary isn't preferred? like maybe: survRates.adultRates = [1.5, 3.5, 2.1] or something and then another key with survRates.unknownKey = [[], [], []] it seems like it will confuse you less often. To remember how often to nest: either way your gens() code makes a balanced list of lists because it generated in a simple loop... if you really want the first element in the list to be special exclude it from this loop.
Sorry about the confusion. I ended up using a simple loop like my gens code, and it seemed to work. Thanks for the help anyway
0

Your question appears to be how to initialize survRates.

First, you create a list the is three elements long, with each element being an empty list.

    survRates = [[],[],[]]

This means:

  survRates[0] = []

Next, each element of the list is changed from a list to a float, instead of appending the float element to the list.

  survRates[0] = 1.5

To create survRates = [[1.5],[3.5],[2.5]] change survRates[0] = 1.5 to survRates[0].append(1.5). This appends the number to the empty list.

Comments

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.