尝试通过ArcGIS 10.1中的UpdateCursor执行一些看似简单的字段计算,并获得有关无法迭代浮点数的错误。这是我的代码 - 有些东西被注释掉了b / c它对我的问题不重要所以请忽略它。
#import arcpy module
import arcpy
#doing some fancy math
import math
#message to let you know the script started
print "Begin Field Calculation for age-adjusted-rate."
#input shapefile
inputFC = 'C:\\blahblah.shp'
#variable to define the new field name
Field_Name = ['Age_Adj_R', 'wt_1', 'wt_2', 'wt_3']
#add the new Fields
#arcpy.AddField_management(inputFC, Field_Name[0], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inputFC, Field_Name[1], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inputFC, Field_Name[2], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inputFC, Field_Name[3], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#list variable for the fields in the table that will be used
fields = ["Cnt1", "Cnt2", "Cnt3", "Pop1", "Pop2", "Pop3", "Crude_Rate", "Age_Adj_R", "wt_1", "wt_2", "wt_3"]
#wt_age_avg = [0.2869, 0.5479, 0.1652]
#populate the weighted average fields
cursor = arcpy.da.InsertCursor(inputFC, ["wt_1", "wt_2", "wt_3"])
for x in xrange(0, 51):
cursor.insertRow([0.2869, 0.5479, 0.1652])
del cursor
#function to perform the field calculation using an update cursor
with arcpy.da.UpdateCursor(inputFC, fields) as cursor:
for row in cursor: #iterate through each row
if not -99 in row: #check for missing values
row[7] = str(sum((row[6]) * ((row[0] * row[8]) + (row[1] * row[9]) + (row[2] * row[10]))) #do the calculation
else:
row[7] = 0 #missing values found, place a null response
cursor.updateRow(row) #save the calculations
del row #release the variables
#acknowledge completion
print "Calculation Completed."
IDLE中的错误:
Traceback (most recent call last):
File "C:\blahblah.py", line 48, in <module>
row[7] = str(sum((row[6]) * ((row[0] * row[8]) + (row[1] * row[9]) + (row[2] * row[10])))) #do the calculation
TypeError: 'float' object is not iterable
好的 - 但我认为我甚至在填充字段之前将其更改为字符串....我不知道如何使这个计算起作用。它应该看起来像:
总和(crude_rate * sum(weighted_averages))
如果我使用常量值字段的方式不起作用,我也尝试将值作为变量传递(参见变量:wt_age_avg)而没有运气。还使用像math.fsum这样的其他求和函数也没有工作。
答案 0 :(得分:0)
sum(iterable[, start])
总和开始以及可迭代的项目 从左到右并返回总数。开始默认为0 iterable的项目通常是数字,而起始值则不是 允许是一个字符串。
但这是有趣的部分:你不需要在这里使用sum()
!你可以改为:
row[7] = str( (row[6] * row[0] * row[8]) +
(row[1] * row[9]) +
(row[2] * row[10]) )
答案 1 :(得分:0)
+
运算符就足够了,您不需要sum()
调用。错误是调用sum(number)
。
答案 2 :(得分:0)
其他答案是正确的,但如果您希望将sum()
用于可读性目的,则可以将值作为列表传递...
row[7] = str(sum([row[6] * row[0] * row[8],
row[1] * row[9],
row[2] * row[10]] ))