Grasshopper

algorithmic modeling for Rhino

Python Runtime error (TypeErrorException): 'generator' object is unsubscriptable

 

Hi everyone

I am trying to create a component which takes a list of lists, the lists being

MEAN_COLUMN (mean) and self.SD_COLUMN (standard deviation).

 

The component will then use the data from these lists by using the function

random.normalvariate(mean,standardDeviation), which will use the normal distribution to create one list for each duplicate that varys from the original data according to the normal distribution.

 

The whole point of this to create realistic variations in electrical load throughout the whole year.

 

I'm not sure how clearly I've explained this but the problem is that I'm getting the error below

Runtime error (TypeErrorException): 'generator' object is unsubscriptable

Traceback:

line 26, in getStandardDeviation, "<string>"

line 44, in calculate, "<string>"

line 54, in script

 

Any help pointing out why I am getting this error and how I can fix it would be most appreciated, also can I ask how to do you create panels on the Grasshopper canvas that hold two lists?

 

 

Views: 3412

Attachments:

Replies to This Discussion

Hi Anton,

The generator ("func" function) does not return a list. That's the way generators work - they yield values, but do not return a list. To "convert" it to a list, enclose it with list() function.

Right click on "baseLoadArray_" and set it to "List Access".
Right click on your input panel and uncheck the "multiline data" (if it's checked then that means you have on big single string).

Other than that the unsubscriptable part which appears later is because you are trying to access an element from a list of lists, but you only have a regular list.

Check the attached file.

Attachments:

Thanks that made a lot of sense and fixed the problem! I appreciate it!

Hi Djordje

 

I have another question I see that in my code you blocked out hour like the code below

 

self.baseLoadArray[self.MEAN_COLUMN]#[hour]

 

when I unblock this code I get the error

 

Runtime error (TypeErrorException): 'float' object is unsubscriptable

Traceback:

line 26, in getStandardDeviation, "<string>"

line 44, in calculate, "<string>"

line 54, in script

 

this is because baseLoadArray_ has only one column of data right? Could you please explain why it is doing this and how I can fix it? also how do I enter data into python panel so that I get two columns (lists of data)

 

Thankyou!

 

 

Yes, that's what I meant when I said:

Other than that the unsubscriptable part which appears later is because you are trying to access an element from a list of lists, but you only have a regular list.

"one column of data" is python regular list. Example:

self.baseLoadArray = [1.6, 2.45 ,4.0 ,6.21 ,8.2]

So you can access each of the self.baseLoadArray items:

self.MEAN_COLUMN = 0
print self.baseLoadArray[self.MEAN_COLUMN]     # 1.6

But not further than that, as you reached to "top subscriptable" level sort to say, because your list items are floats. If they were strings, then you could have accessed them:

self.baseLoadArray = ["1.6", "2.45", "4.0", "6.21", "8.2"]
self.MEAN_COLUMN = 0
hour = 1
print self.baseLoadArray[self.MEAN_COLUMN] [hour]     # "."

So what you need, are list of lists (or tuples, does not matter):

self.baseLoadArray = [[1.6, 2.45 ,4.0 ,6.21 ,8.2], [0,2,4,6,8], [12,10,8,6]]

You can input each of these "subLists" with a separate ghpython input parameters, or you can build a data tree from your input lists, input them to ghpython component, and then "grab" the lists from each data tree branch.
Check the attached file.

Attachments:

Hi Djordje 

Thanks again for your reply I understand what you have done and said above but I am still unable to implement it in the code and I am still having issues with it.

I have now changed the code so that it takes 2 lists these lists are then put through the generator to yield a list of floats for each 

After that I do the following, to form these lists into a list of lists

listOfLists = [listfloats1,listfloats2]

When this is passed into the class it doesnt doesnt work however, I've attached the file I'd appreciate it a lot if you have a look

Attachments:

Your "baseLoadArray_sd" input is not set to "List access".

The length of your "profile" is 8760 items (zeros). You are trying to iterate through self.baseLoadArray[0] from 0 to 8760. The problem is that self.baseLoadArray[0] has only 18 items. So basically once you try to pick the 19th item, you get an error.

I just explained what is the issue, but I do not understand the logic of your code, so can't help you to write it, you would have to do that by yourself.

Hi Djordje

Ive got it thanks for your help!

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service