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 のような他の合計関数を使用しても機能しませんでした。