<?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: Compare Two CSV Files; Understanding DICT Statement in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033770#M60272</link>
    <description>&lt;P&gt;So in the enumerate function, it has 2 components - 'i' and 'r' (these are just 'made up' references which could be anything - we just give them a simple name).&lt;/P&gt;&lt;P&gt;In this case, i is the index of the csv row, r is the entire row. r[1] is the 2nd cell in the CSV row (indexes start at 0!).&lt;/P&gt;&lt;P&gt;Enumerate is a Generator function where the r[1] value is being mapped to it's row index in a dictionary.&lt;/P&gt;&lt;P&gt;dict() can be used to construct key-value pair dictionary values:&lt;/P&gt;&lt;P&gt;e.g. dict( [('a':1), ('b':2)] )&amp;nbsp;&lt;/P&gt;&lt;P&gt;so the big list of row[1] values and their corresponding row index is going in to the dictionary constructor.&lt;/P&gt;&lt;P&gt;Not explained very well at all&amp;nbsp; I think &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 07 Mar 2021 17:55:31 GMT</pubDate>
    <dc:creator>DavidPike</dc:creator>
    <dc:date>2021-03-07T17:55:31Z</dc:date>
    <item>
      <title>Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033769#M60271</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Understanding &lt;/STRONG&gt;&lt;STRONG&gt;Python d&lt;/STRONG&gt;&lt;STRONG&gt;ict Statement&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to Python, so forgive me if I’m asking questions about what should be obvious. If this is posted in the wrong place, my apologies: please let me know where I should be posting this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using Python 2.7.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am re-using and modifying 2014 code that was written by Martjin Pieters and posted to &lt;A href="https://stackoverflow.com/" target="_blank" rel="noopener"&gt;StackOverflow&lt;/A&gt; [message 5268929]. It allows for the comparison of two CSV files, reporting on the differences between the master and host files. I thank him for this head start. I am comparing a file exported from ArcGIS using ArcPy. Here it is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;1 #2/3/2021&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;2 #Source: Stack Overflow "Compare two CSV files and search for similar items”&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;4 import csv&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;5&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;6 with open('masterlist.csv', 'rb') as master:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;&lt;SPAN&gt;7 &lt;/SPAN&gt;&lt;SPAN&gt;master_indicies = dics ((r[1], i) for i, r in enumerate(csv.reader(master)))&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;8&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;9 with open('hosts.csv', 'rb') as hosts:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;10 with open('results.csv', 'wb') as results:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;11 reader = csv.reader(hosts)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;12 writer = csv.writer(results)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;13&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;14 for row in reader:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;15 index = master_indices.get(row[3])&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;16 if index is not None:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;17 message = 'FOUND in master list (row {})' .format(index)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;18 else:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;19 message = 'NOT FOUND in master list'&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Arial, sans-serif"&gt;&lt;FONT size="2"&gt;20 writer.writerrow(row + [message])&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I sort of understand most of it save one section of line 7 shown in the figure below (though not sure I’ve parsed [the colors] the components correctly):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-02-24_9-57-00.png" style="width: 693px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/7761i6FB5E078C98E424A/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-02-24_9-57-00.png" alt="2021-02-24_9-57-00.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The part highlighted in green is the one element of the &lt;STRONG&gt;dict &lt;/STRONG&gt;entry I simply do not know how to read/interpret [what is ‘r’?; what is ‘i’ and what does ‘i) for i’ do; etc.]. If someone could point me to a place that I could learn about dict at &lt;U&gt;an introductory level&lt;/U&gt;, hopefully with enough specificity that would help me to sort this out; that would be great. Just trying to get my head around this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Mar 2021 17:11:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033769#M60271</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-03-07T17:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033770#M60272</link>
      <description>&lt;P&gt;So in the enumerate function, it has 2 components - 'i' and 'r' (these are just 'made up' references which could be anything - we just give them a simple name).&lt;/P&gt;&lt;P&gt;In this case, i is the index of the csv row, r is the entire row. r[1] is the 2nd cell in the CSV row (indexes start at 0!).&lt;/P&gt;&lt;P&gt;Enumerate is a Generator function where the r[1] value is being mapped to it's row index in a dictionary.&lt;/P&gt;&lt;P&gt;dict() can be used to construct key-value pair dictionary values:&lt;/P&gt;&lt;P&gt;e.g. dict( [('a':1), ('b':2)] )&amp;nbsp;&lt;/P&gt;&lt;P&gt;so the big list of row[1] values and their corresponding row index is going in to the dictionary constructor.&lt;/P&gt;&lt;P&gt;Not explained very well at all&amp;nbsp; I think &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Mar 2021 17:55:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033770#M60272</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-07T17:55:31Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033771#M60273</link>
      <description>&lt;P&gt;On the surface, r would be the values (data) returned in the csv.reader(master)&lt;/P&gt;&lt;P&gt;i is the enumerated value (0,1, 2, 3, 4 etc) of that data.&lt;/P&gt;&lt;P&gt;i, r is the tupled (index, data) result of the enumerate(csv.reader(master)) function.&lt;/P&gt;&lt;P&gt;dict((r[1], i) for i, r in enumerate(csv.reader(master))) is a form of list comprehension that is creating a dict from the returned value.&lt;/P&gt;&lt;P&gt;edit to add:&lt;/P&gt;&lt;P&gt;(r[1], i) for i, r in enumerate(csv.reader(master)) creates a list of tuples for the dict to create a dictionary.&lt;/P&gt;&lt;P&gt;The value from position r[1] would be the key and the i as the value.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Mar 2021 18:04:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033771#M60273</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-03-07T18:04:46Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033780#M60274</link>
      <description>&lt;P&gt;Just for learning purposes, it is helpful to look to the documentation:&amp;nbsp;&amp;nbsp;&lt;A href="https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary%20constructor#dictionaries" target="_blank" rel="noopener"&gt;5.5 Dictionaries -- Data Structures -- Python 3.9.2 documentation&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;The dict() constructor builds dictionaries directly from sequences of key-value pairs&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Using an annotated, multi-line format to represent the single-line of code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;iterable = csv.reader(master)                     # CSV reader object is an iterable

dictionary = dict(                                # dictionary constructor
    (                                             # generator expression
        (count, value)                            # key-value pair
        for count, value in                       # for comprehension
        enumerate(                                # enumerating function
            iterable                              # iterable or sequence
        )
    )
)

master_indices = dictionary                       # master_indicies is a dictionary&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 07 Mar 2021 18:35:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033780#M60274</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-03-07T18:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033781#M60275</link>
      <description>&lt;P&gt;The green part is basically a for-loop, though formatted differently. The two bits of code below are &lt;EM&gt;essentially&lt;/EM&gt; equivalent.&lt;/P&gt;&lt;PRE&gt;new_list = []&lt;BR /&gt;&lt;BR /&gt;for item in list_of_items:&lt;BR /&gt;    empty_list.append(str(item))&lt;/PRE&gt;&lt;PRE&gt;new_list = [str(item) for item in list_of_items]&lt;/PRE&gt;&lt;P&gt;The first &lt;EM&gt;feels&lt;/EM&gt; a lot clearer and easier to follow, especially if you're new to Python, but the second is much more efficient. Still, they both end at the same point.&lt;/P&gt;&lt;P&gt;But supposing you're using that variable&amp;nbsp;&lt;STRONG&gt;new_list&lt;/STRONG&gt; simply as an input to another function, now the first code sequence creates a useless variable that's going to hang out for the rest of your code, taking up memory. The second makes it possible to avoid this.&lt;/P&gt;&lt;P&gt;@Anonymous Userand &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt; have explained the rest of it well, I think, but list comprehension can get &lt;EM&gt;very &lt;/EM&gt;complex, and if you're not familiar with them, I'd hardly blame you for being a bit thrown by them. I still don't use them right half the time.&lt;/P&gt;&lt;P&gt;Check out some of the examples &lt;A href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" target="_self"&gt;in the Python docs&lt;/A&gt; to get a clearer picture of what they are and how they can be used.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Mar 2021 18:52:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033781#M60275</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-03-07T18:52:48Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033782#M60276</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;&amp;nbsp;Indeed! the generator inside of the list comprehension alongside the dict() method did need a long think.&amp;nbsp; I also think a lot of my own stuff can be put in a one-liner but I just feel I know what's going on in the code a bit more logically when I make it verbose, and I 'trust' it more.&amp;nbsp; It's something I need to start making an effort to understand/implement if I want to get better.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Mar 2021 19:08:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1033782#M60276</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-07T19:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compare Two CSV Files; Understanding DICT Statement</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1034700#M60320</link>
      <description>&lt;P&gt;Thank you all for being so kind to take the time to reply.&amp;nbsp; I'm working my way through your responses and already seeing some daylight.&amp;nbsp; I'll post later when I get my head around this in a useful way.&amp;nbsp; Doing a couple of simple scripts and work up from there.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 21:22:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-csv-files-understanding-dict-statement/m-p/1034700#M60320</guid>
      <dc:creator>WilliamCole</dc:creator>
      <dc:date>2021-03-09T21:22:37Z</dc:date>
    </item>
  </channel>
</rss>

