2013-04-08 31 views
2

嘗試在ArcGIS 10.1中通過UpdateCursor執行一些看似簡單的字段計算,並獲取有關無法迭代浮動的錯誤。這是我的代碼 - 有些東西被註釋掉了,它對我的​​問題並不重要,所以只是忽略它。arcpy TypeError:'float'對象不可迭代

#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 *總和(weighted_averages))

如果我使用常量的值字段不工作的方式,我也試圖傳遞值作爲變量(見變量:wt_age_avg )沒有運氣。另外使用其他求和功能,如math.fsum也沒有工作。

+0

'打印(行)'它打印出來的清單? – User 2013-04-08 18:19:54

回答

0

sum() expects an iterable

sum(iterable[, start])
Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.

但這裏有一個有趣的部分:你不需要在這裏使用sum()!你可以這樣做,而不是:

row[7] = str((row[6] * row[0] * row[8]) + 
       (row[1] * row[9]) + 
       (row[2] * row[10])) 
+0

嗯伯尼,這不是我正在尋找的東西,但你已經給了我一個如何解決它的想法... – geneari 2013-04-08 18:24:43

+0

@geneari:我們很喜歡那些想法如何解決這裏的東西的人。歡迎來到SO,希望我們能看到更多的你。保重。 – bernie 2013-04-08 18:30:47

+0

對不起,我只有幾個小時的愚蠢行爲纔會這麼做。你讓我看到它是一種不同的(因此是正確的)方式。謝謝。 – geneari 2013-04-08 18:30:50

0

+操作就足夠了,你不需要sum()電話。錯誤號是sum(number)

0

其他的答案是正確的,但如果您想使用sum()爲了提高可讀性,你可以通過你的價值觀作爲一個列表...

row[7] = str(sum([row[6] * row[0] * row[8], 
        row[1] * row[9], 
        row[2] * row[10]])) 
+1

基本,這是一個很好的建議供將來參考,謝謝! – geneari 2013-04-08 20:08:35