I am having an issue of creating an array of floats. I have a loop that is scanning the rows of an attribute table and extracting numbers from it. SARAarea, truearea and rasterarea are all extracted from an attribute table and they should be floats. I am trying to use these numbers and do calculations with them and then insert them back into the attribute table. My main issue is adding floats to the list. I keep getting error messages that a float is not iterable or a float is not an attribute to append or extend. What is the best way to do this? I tried to create one loop that would getvalue and insert value into the attribute without using a list. But the searchcursor function only allows me to create two separate loops because there is a seachCursor and a UpdateCursor, it only allows me to do one task at a time.
This is my attempt at initializing the lists
approxTen=['f'] approxremaining=['f'] lessperc=['f'] moreperc=['f']
This is where I am getting my errors. The code is in a while loop of a searchCursor. "While row:".
approxTen=(SARAarea*truearea)/rasterarea approxremaining=truearea-approxTen lessperc=(approxTen/truearea)*100 moreperc=(approxremaining/truearea)*100 approxTen.append(approxTen) approxremaining.append(approxremaining) lessperc.append(lessperc) moreperc.append(moreperc)
Common error I am getting below. Line 50 is line 7 in the code above. I have tried extend and append but neither worked and I haved tried putting float() around the values of approxTen and the rest of them but it did not work.
For example: approxremaining= float(truearea-approxTen)
This is how I am inserting it back into the attribute table. Below is a separate loop then above code. I am trying to loop through the list and add it to the attribute table.
while row3: row3=approxTen[row3] row3=approxremaining[row3] row3=lessperc[row3] row3=moreperc[row3] cursoru.updateRow(row3)
isn't a list
approxTen = [(SARAarea * truearea) / float(rasterarea)]
is a list to which you can append or extend to (notice the [ ] ) and there is a small added floating point check to ensure that you don't perform unwanted integer division. This also assumes that rasterarea is not 0 (otherwise add value checks for that condition.
is assigning approxTen to a float, or possibly an int depending on the types of variables in the equation. Since approxTen is a float, you can't append something to it because floats don't have append or extend methods. Going further back in your code, your code to initialize a list of floats is not doing what you think:
The code as written is creating a list with a single item, the item being a string with a single character, 'f'. You can initialize a float array, but the syntax is different:
>>> import array >>> approxTen = array.array('f') >>> approxTen array('f') >>>
Lists are more common in Python than arrays because of their flexibility with data types and syntactic sugar for working with them, like list comprehensions. There is a case to be made for working with arrays but that is usually when performance is a primary concern.
I switched it to an array and it works very well. But I am having an issue inputting values on the attribute table. My code is receiving no errors but I believe it is only inputting the values at the end of the array.
I have it looping 4 times but it is only filling in one row of the table. What is the main issue going on?
You will need to post more code, not necessarily all, but more of the functional parts. With the snippets you have posted here, it is hard to provide specific suggestions. The structure of your code snippet, i.e., the way you are using a while loop, is not very idiomatic. As best I can tell, even though you may be looping over row3, you are not moving the cursor, so you appear to be continually updating the same row.