1

I'm looking for some help in explaining what exactly is occurring with the following code:

MaterialLife = "LifeMaterial(!"+Material+"!)"
codeblock2 = """def LifeMaterial(mat):
    material = {"ABS":90,"AC":100,"BRICK":100,"CI":70,"CLAY":100,
          "CONC":100,"CONPP":100,
          "CONR":100,"CONX":100,
          "CORI":70,"CORM":90,
          "CSP":70,"CSPA":90, "CSPH":70,
          "CSPP":70,"DI":60,"PB":80,
          "PE":80,"PP":80,"PVC":100,
          "PVCC":100,"ST":80,"STC":100,
          "STONE":100,"TP":40,"PFR":90,
          " ":90,"UNK":90,None:90}

    life = material [mat]
    return life"""

arcpy.CalculateField_management(ins_pipe, MaterialLifetime, MaterialLife, "PYTHON_9.3", codeblock2)

I know the basic structure in that there's an expression (MaterialLife) and a codeblock (codeblock2) that are used to perform a calculate field function. The input field to the expression (Material or mat in the codeblock) contains all of the different types of materials that are in the codeblock ("CONC", "CONPP" etc.). The numbers beside each of the materials are ages that are to be assigned to each material. The end result of the function is that all of the ages associated to the material types are populated in the field "MaterialLifetime" based on its corresponding Material type.

My main questions are:

  • How does the program know to properly go through every entry in the table even though they are not listed in the order that is laid out in the codeblock without any if statements?
  • How does the program know to populate the "MaterialLifetime" field with ONLY the age and not the material type as well?

closed as too broad by PolyGeo Feb 28 at 20:02

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • As per the Tour there should be only one question asked per question. – PolyGeo Feb 28 at 20:02
2

That is a dictionary, not a list. You provide a key, for example "ABS" like this: material["ABS"] and you get the value 90 returned. Very useful instead of many if statements.

The key is in one of your fields and the return value is used to populate another.

(I recommend you use the da.UpdateCursor instead of Field Calculator. More versatile and easier to use)

Not the answer you're looking for? Browse other questions tagged or ask your own question.