2

尝试通过 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

好的——但我想我在它甚至会填充字段之前将它更改为一个字符串......我不知道如何让这个计算工作。它应该看起来像:

sum(crude_rate* sum(weighted_averages))

如果我使用常量值字段的方式不起作用,我也尝试将值作为变量传递(请参阅变量:wt_age_avg),但没有运气。也使用其他求和函数,如 math.fsum 也不起作用。

4

3 回答 3

0

sum()期望一个可迭代的

sum(iterable[, start])
总和从左到右开始和可迭代 的项目并返回总数。start 默认为 0。iterable 的项一般为数字,start 值不允许为字符串。

但有趣的是:你不需要在sum()这里使用!你可以这样做:

row[7] = str( (row[6] * row[0] * row[8]) + 
              (row[1] * row[9]) + 
              (row[2] * row[10]) )
于 2013-04-08T18:21:14.767 回答
0

接线员就够了,+不需要sum()打电话。错误正在调用sum(number)

于 2013-04-08T18:21:23.520 回答
0

其他答案是正确的,但如果您希望sum()用于可读性目的,您可以将您的值作为列表传递......

row[7] = str(sum([row[6] * row[0] * row[8],
                  row[1] * row[9],
                  row[2] * row[10]] ))
于 2013-04-08T18:30:43.703 回答