Hello,
Here's the code I wrote to do the following:
1. Assign a min and max value to multiple fields on multiple feature classes.
2. The Min and Max values have to correlate with the min and max value in a different field on the same feature class.
For example:
A. Select the min value from the MEDIAN_HOUSEHOLD_INCOME_2018 field and assign a specific value to all of the other fields in the feature class. All of the values are different for each field and each feature class.
B. Do the same but for the max value of the MEDIAN_HOUSEHOLD_INCOME_2018 field
I am having trouble with the loops when assigning the Construction costs to all of the different feature classes.
I am creating too many loops and I don't know what are really necessary and which ones I don't need.
I would appreciate any help with this,
I am learning and have tried to fix it for a day and haven't been able to identify how to do it,
I am aware that there is a major problem with the iteration of the different lists,
thank you!
import operator
import arcpy
arcpy.env.workspace = r"D:\APRX_MXDS\FAR_Initialization_Project\10_2020_Linear_Regression_USA\construction_costs\test_gdb.gdb"
arcpy.env.overwriteOutput = True
feature_class_list = []
list_fc = arcpy.ListFeatureClasses()
for m in list_fc:
feature_class_list.append(m)
print(feature_class_list)
field_list = ["MEDIAN_HOUSEHOLD_INCOME_2018", "CONST_PRICE_PER_SF_CONDO", "CONST_PRICE_PER_SF_MULTIFAM", "CONST_PRICE_PER_SF_OFFICE", "CONST_PRICE_PER_SF_RETAIL", "CONST_PRICE_PER_SF_HOTEL",
"CONST_PRICE_PER_SF_INDUSTRIAL", "CONST_PRICE_PER_SF_PARKING"]
fc_list = ['Atlanta', 'Boston', 'Chicago', 'Dallas', 'Denver', 'Honolulu', 'Las_Vegas', 'Los_Angeles', 'Miami', 'Nashville', 'New_York', 'Orlando',
'Philadelphia', 'Portland', 'Raleigh', 'Sacramento', 'San_Diego', 'San_Francisco', 'Seattle', 'Washington']
max_list = []
min_list = []
for fc in fc_list:
print("Starting with feature class: {}".format(fc))
all_entries = [row for row in arcpy.da.SearchCursor(fc, ['MEDIAN_HOUSEHOLD_INCOME_2018'])]
maximum = (max(all_entries)[0])
minimum = (min(all_entries)[0])
max_list.append(maximum)
min_list.append(minimum)
print("Maximum Values List: {}".format(max_list))
print("Minimum Values List: {}".format(min_list))
max_construction_lists = [[394, 394, 674, 452, 708, 589, 139], [510, 510, 874, 586, 912, 763, 180], [534, 534, 914, 613, 906, 798, 188],
[383, 383, 657, 440, 685, 573, 135], [314, 314, 485, 461, 717, 600, 142], [537, 537, 690, 617, 960, 803, 189],
[469, 469, 803, 538, 838, 701, 165], [529, 529, 688, 607, 945, 790, 186], [379, 379, 648, 435, 676, 566, 133],
[390, 390, 667, 447, 662, 583, 186], [859, 859, 1026, 687, 1108, 895, 211], [372, 372, 637, 427, 664, 556, 131],
[512, 512, 876, 587, 914, 765, 180], [445, 445, 661, 511, 796, 666, 157], [373, 373, 641, 429, 673, 559, 132], [463, 463, 624, 536, 655, 695, 125],
[503, 503, 678, 583, 712, 755, 180], [584, 584, 1001, 671, 1044, 874, 168], [469, 469, 696, 538, 838, 701, 175], [418, 418, 716, 480, 747, 625, 147]]
min_construction_lists = [[303, 303, 245, 253, 333, 152, 50], [392, 392, 318, 327, 532, 196, 65], [411, 411, 332, 343, 483, 206, 68],
[295, 295, 239, 246, 336, 148, 49], [133, 133, 135, 258, 364, 155, 51], [413, 413, 335, 345, 480, 207, 8],
[361, 361, 292, 301, 354, 180, 60], [294, 294, 327, 165, 425, 203, 49], [230, 230, 235, 242, 351, 146, 48],
[300, 300, 243, 250, 353, 150, 49], [461, 461, 373, 384, 625, 231, 76], [190, 190, 231, 238, 345, 143, 47],
[393, 393, 319, 328, 534, 197, 65], [343, 343, 277, 286, 358, 171, 65], [288, 288, 233, 240, 317, 144, 4],
[251, 251, 289, 150, 320, 178, 61], [273, 273, 325, 163, 348, 194, 66], [449, 449, 364, 375, 555, 225, 79],
[361, 361, 292, 301, 376, 180, 68], [322, 322, 260, 268, 436, 161, 53]]
for fc in fc_list:
with arcpy.da.UpdateCursor(fc, field_list) as cursor:
for row in cursor:
for max_income in max_list:
print(max_income)
for lists in max_construction_lists:
print(lists)
if row[0] == max_income:
row[1] = lists[0]
print("This should be the condo max value for each city: {}".format(lists[0]))
row[2] = lists[1]
row[3] = lists[2]
row[4] = lists[3]
row[5] = lists[4]
row[6] = lists[5]
row[7] = lists[6]
cursor.updateRow(row)
print("Done updating max construction costs in all cities")
for fc in fc_list:
with arcpy.da.UpdateCursor(fc, field_list) as cursor:
for row in cursor:
for min_income in min_list:
for lists in min_construction_lists:
if row[0] == min_income:
row[1] = lists[0]
row[2] = lists[1]
row[3] = lists[2]
row[4] = lists[3]
row[5] = lists[4]
row[6] = lists[5]
row[7] = lists[6]
cursor.updateRow(row)
print("Done updating min construction costs in all cities")