<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Troubleshooting a loop utilizing a cursor and a dictionary to write to a table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724581#M56102</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That was certainly the problem!&amp;nbsp; I modified the code with the copy package so BMP = copy.deepcopy(D) and the problem resolved. Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 20 May 2015 21:36:22 GMT</pubDate>
    <dc:creator>RachaelJohnson</dc:creator>
    <dc:date>2015-05-20T21:36:22Z</dc:date>
    <item>
      <title>Troubleshooting a loop utilizing a cursor and a dictionary to write to a table</title>
      <link>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724579#M56100</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am comparing the values in one table to the values in another table.&amp;nbsp; I converted the values of one table into a dictionary to avoid using nested cursors.&amp;nbsp; Ultimately, I want to create a table of items that pass each comparison test.&amp;nbsp; I did another version of this where I have separate tables for each area I am testing, start with a full table, and remove rows from the table as each row fails a test.&amp;nbsp; This results in many tables, which is fine for a handful of areas but messy for more than a few.&amp;nbsp; I am trying to build a version that generates all area outputs in the same table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think it is easier to keep track of those that fail tests than those that pass.&amp;nbsp; I copy the main dictionary, D, to a new dictionary.&amp;nbsp; I create a blank dictionary for the failed tests.&amp;nbsp; Each time something fails a test, I record its key into the Failed Test dictionary and store the reason for failure as a string value.&amp;nbsp; For subsequent tests, if the key already exists in the Failed test dictionary, I add on to the existing value as a pseudo-list to prevent test failures from being overwritten.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When all tests are complete, I want to compare the keys in the Failed Test dictionary to the copy of D.&amp;nbsp; If the keys are a match, I want that key and it's associated value removed from the copy of D.&amp;nbsp; What should remain in the copy of D are the BMPs that pass all 4 tests.&amp;nbsp; Then I want to use the values stored in the copy of D to write to a table.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Maybe this logic is all sorts of messed up and there's a better way to do it.&amp;nbsp; I am open to suggestions.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have been playing with this code for a while to figure out what the problem is and it seems I have found the first source of error in my code.&amp;nbsp; There appears to be a problem with copying the dictionary, D.&amp;nbsp; Here's the code I have so far.&amp;nbsp; It is missing the write-to-table part because I am not getting the outputs I would expect. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print("Comparing site values to constraints...")
# Compare the lumped parameters to the constraint dictionary
for row in arcpy.da.SearchCursor(combo_table, ["DARAS", "HSG", "MEDSLOPE", "MEDWT"]):
&amp;nbsp;&amp;nbsp;&amp;nbsp; BMP = {}
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Temporarily store the area of each DA for later comparison
&amp;nbsp;&amp;nbsp;&amp;nbsp; for r in arcpy.da.SearchCursor(DA_area, [DAID, "SUM_AREA"]):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # NOTE: THE DAID/DARAS MISMATCH COULD BE A PROBLEM LATER. NEED TO TEST.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if r[0] == row[0]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; totalArea = r[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Duplicate criteria dictionary that can be amended throughout the loop
&amp;nbsp;&amp;nbsp;&amp;nbsp; print D
&amp;nbsp;&amp;nbsp;&amp;nbsp; BMP = D
&amp;nbsp;&amp;nbsp;&amp;nbsp; print D
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Initialize empty dictionary to store BMPs that fail each test
&amp;nbsp;&amp;nbsp;&amp;nbsp; NoBMP = {}
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Compare lumped values in each DA/HSG pair to those in the constraint table
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(row[0], row[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; for k, v in D.items():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Test if soil type is incorrect for each BMP
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row[1] not in v:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add failed BMP to the failed BMP dictionary, NoBMP
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NoBMP&lt;K&gt; = "Soil Type Mismatch"&lt;/K&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Compare keys in BMP and NoBMP dictionaries. Remove matching pairs from the BMP dictionary.
&amp;nbsp;&amp;nbsp;&amp;nbsp; for key in BMP.keys():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if key in NoBMP.keys():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del BMP[key]&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The main takeaway from the following output information is that D changes sizes from one iteration of the Cursor to the next iteration of the Cursor; D does not change size in future iterations of the loop.&amp;nbsp; I think it has something to do with me changing the size of BMP at the end of the code by deleting the matching keys.&amp;nbsp; The keys that are removed are the keys that do not show up in the next iteration.&amp;nbsp; However, I reinitialize BMP as a blank dictionary at the beginning of the Cursor, so I am not sure what else to do.&amp;nbsp;&amp;nbsp; I have also tried BMP = dict() to reinitialize and gotten the same result.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;Comparing site values to constraints...&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;{u'WPGW1': u'C,D', u'WPGW2': u'C,D', u'VFA': u'A', u'SDCD': u'C,D', u'BRE2': u'A,B,C,D', u'BRE1': u'A,B,C,D', u'SDAB': u'A,B', u'EDP2': u'A,B,C,D', u'WP1': u'C,D', u'CW1': u'C,D', u'CW2': u'C,D', u'DS2': u'A,B,C,D', u'DS1': u'A,B,C,D', u'GCCD': u'C,D', u'F2': u'A,B,C,D', u'F1': u'A,B,C,D', u'CAAB': u'A,B', u'WP2': u'C,D', u'GCAB': u'A,B', u'CACD': u'C,D', u'VFSA': u'B,C,D', u'SI2': u'A,B', u'SI1': u'A,B', u'EDP1': u'A,B,C,D', u'SDSA': u'C,D', u'WS1': u'C,D', u'CI2': u'A,B', u'CI1': u'A,B', u'WS2': u'C,D', u'GCSA': u'C,D', u'MI1': u'A,B', u'MI2': u'A,B'}&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;{u'WPGW1': u'C,D', u'WPGW2': u'C,D', u'VFA': u'A', u'SDCD': u'C,D', u'BRE2': u'A,B,C,D', u'BRE1': u'A,B,C,D', u'SDAB': u'A,B', u'EDP2': u'A,B,C,D', u'WP1': u'C,D', u'CW1': u'C,D', u'CW2': u'C,D', u'DS2': u'A,B,C,D', u'DS1': u'A,B,C,D', u'GCCD': u'C,D', u'F2': u'A,B,C,D', u'F1': u'A,B,C,D', u'CAAB': u'A,B', u'WP2': u'C,D', u'GCAB': u'A,B', u'CACD': u'C,D', u'VFSA': u'B,C,D', u'SI2': u'A,B', u'SI1': u'A,B', u'EDP1': u'A,B,C,D', u'SDSA': u'C,D', u'WS1': u'C,D', u'CI2': u'A,B', u'CI1': u'A,B', u'WS2': u'C,D', u'GCSA': u'C,D', u'MI1': u'A,B', u'MI2': u'A,B'}&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;(1, u'D')&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;{u'WPGW1': u'C,D', u'WPGW2': u'C,D', u'SDCD': u'C,D', u'BRE2': u'A,B,C,D', u'BRE1': u'A,B,C,D', u'EDP2': u'A,B,C,D', u'WP1': u'C,D', u'CW1': u'C,D', u'CW2': u'C,D', u'DS2': u'A,B,C,D', u'DS1': u'A,B,C,D', u'GCCD': u'C,D', u'F2': u'A,B,C,D', u'F1': u'A,B,C,D', u'WP2': u'C,D', u'CACD': u'C,D', u'VFSA': u'B,C,D', u'EDP1': u'A,B,C,D', u'SDSA': u'C,D', u'WS1': u'C,D', u'WS2': u'C,D', u'GCSA': u'C,D'}&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;{u'WPGW1': u'C,D', u'WPGW2': u'C,D', u'SDCD': u'C,D', u'BRE2': u'A,B,C,D', u'BRE1': u'A,B,C,D', u'EDP2': u'A,B,C,D', u'WP1': u'C,D', u'CW1': u'C,D', u'CW2': u'C,D', u'DS2': u'A,B,C,D', u'DS1': u'A,B,C,D', u'GCCD': u'C,D', u'F2': u'A,B,C,D', u'F1': u'A,B,C,D', u'WP2': u'C,D', u'CACD': u'C,D', u'VFSA': u'B,C,D', u'EDP1': u'A,B,C,D', u'SDSA': u'C,D', u'WS1': u'C,D', u'WS2': u'C,D', u'GCSA': u'C,D'}&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;(2, u'D')&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:59:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724579#M56100</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2021-12-12T06:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: Troubleshooting a loop utilizing a cursor and a dictionary to write to a table</title>
      <link>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724580#M56101</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am not suggesting the following will fix your overall issue, but I do have a comment regarding dictionaries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You speak of "copying the dictionary" and there is a code comment to "duplicate" the dictionary.&amp;nbsp; Where are you making this copy?&amp;nbsp; Line 12?&amp;nbsp; Line 12 is not copying dictionary D, you are just binding another variable to the same dictionary.&amp;nbsp; Python &lt;A href="https://docs.python.org/2/library/stdtypes.html#dict"&gt;dictionaries &lt;/A&gt;have a copy() method for returning a shallow copy of themselves. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 May 2015 20:54:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724580#M56101</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-05-20T20:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: Troubleshooting a loop utilizing a cursor and a dictionary to write to a table</title>
      <link>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724581#M56102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That was certainly the problem!&amp;nbsp; I modified the code with the copy package so BMP = copy.deepcopy(D) and the problem resolved. Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 May 2015 21:36:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/troubleshooting-a-loop-utilizing-a-cursor-and-a/m-p/724581#M56102</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-05-20T21:36:22Z</dc:date>
    </item>
  </channel>
</rss>

