Script not working as expected

المشرف العام

Administrator
طاقم الإدارة
I'm newbie to python and I wrote this script. It is supposed to check the text in the fields fld_o and fld_d and write a value (just numbers in a sequence) to a newly created field fld_name. While looping if it comes across a text in either of the two fields which was already there is any of the previous rows in either of the fields then it should write the same value from that previous row to this row in the new field. The script ran without problems. But somehow the results are not as expected. You can see the two rows highlighted both have Werl in common so how can they have a different number in the new field? When the loop reaches the first highlighted row it should add Werl to the dictionary and assign it the same value as Hamm i.e. 54 and write it to the new field and then later when it reaches the second highlighted row it should take the already assigned value of 54 from Werl and assign this to Wickede and write it in the new field as well. At least that's what I expected.

import arcpyarcpy.env.overwriteOutput = Truefc_in = 'E:/SUT/Thesis/Geodata/NRW_Gemeinde_SpatialJoin_SelectCopy.shp'fld_o = 'GEN'fld_d = 'GEN_1'fld_name1 = 'Sub_Region'arcpy.AddField_management(fc_in, fld_name1, 'SHORT') dct = {}sub_region = 0with arcpy.da.UpdateCursor(fc_in, (fld_o, fld_d, fld_name1)) as cursor: for row in cursor: origin = row[0] dest = row[1] if origin in dct and dest in dct: row[2] = dct[origin] elif origin in dct: row[2] = dct[origin] dct[dest] = dct[origin] elif dest in dct: row[2] = dct[dest] dct[origin] = dct[dest] else: sub_region = sub_region + 1 row[2] = sub_region dct[origin] = sub_region dct[dest] = sub_region cursor.updateRow(row)




أكثر...
 
أعلى