<?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: Counting in loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046971#M60781</link>
    <description>&lt;P&gt;Since there is a bit of confusion over what you are trying to do within your loop, I suggest you step back from tools and solutions and explain a bit what your requirement is, i.e., just describe what you are trying to achieve overall.&lt;/P&gt;</description>
    <pubDate>Wed, 14 Apr 2021 14:52:41 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2021-04-14T14:52:41Z</dc:date>
    <item>
      <title>Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046863#M60777</link>
      <description>&lt;P&gt;Good day everybody,&lt;/P&gt;&lt;P&gt;I am currently working with a feature class. I have a field with each cat that could be repeated around 100 or more times. However, I have 25 different cats.&lt;/P&gt;&lt;P&gt;My task is to count how many times each cat appears in the field. Currently, I have a total number 40000 when I use "numbermyList = len(myList)" after my loop and only one when I use it in the loop. When I use "numbermyList = len(cat)", it gives the total number only for the first cat but not working the other cats.(If i use inside or outside of the loop)&lt;/P&gt;&lt;P&gt;Could you please recommend to me how to use the loop to count the number of each cat in my field?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Certainly, I would like to use the received number in my next loop, so I will receive something like this: 1st cat = 4000(numbermyList = len(myList)), 2sd cat 5000 and so on (numbermyList = len(myList)).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 09:05:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046863#M60777</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-04-18T09:05:53Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046956#M60779</link>
      <description>&lt;P&gt;You can use a &lt;A href="https://docs.python.org/3/library/collections.html#collections.defaultdict" target="_self"&gt;default dictionary&lt;/A&gt; for this!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from collections import defaultdict

fakeCursor = ["one", "three", "two", "one", "one", "two", "five", "nine"]

counter = defaultdict(int)

for i in fakeCursor:
    counter[i] += 1

print counter&lt;/LI-CODE&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;defaultdict(&amp;lt;type 'int'&amp;gt;, {'nine': 1, 'five': 1, 'three': 1, 'two': 2, 'one': 3})&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 14:40:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046956#M60779</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-04-14T14:40:52Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046957#M60780</link>
      <description>&lt;P&gt;Can you elaborate a bit on this? Do you mean you want the count of occurrences per cat, something like:&lt;/P&gt;&lt;PRE&gt;cat 1: 100&lt;BR /&gt;cat 2: 2500&lt;BR /&gt;etc...&lt;/PRE&gt;&lt;P&gt;I think part of the problem here is that the&amp;nbsp;&lt;STRONG&gt;myList&lt;/STRONG&gt; object is being redefined as an empty list for every iteration.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 14:41:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046957#M60780</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-04-14T14:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046971#M60781</link>
      <description>&lt;P&gt;Since there is a bit of confusion over what you are trying to do within your loop, I suggest you step back from tools and solutions and explain a bit what your requirement is, i.e., just describe what you are trying to achieve overall.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 14:52:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046971#M60781</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-04-14T14:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046981#M60783</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cat_names = ["cat_1", "cat_2", "cat_3", "cat_25"]
cat_numbers = []

for name in cat_names:
    sql_query = "CatName = '{}'".format(name)
    cat_rows = [c for c in arcpy.da.SearchCursor("Cat_Layer", ["CatName"], sql_query)]
    cat_numbers.append(len(cat_rows))

for name, number in zip(cat_names, cat_numbers):
    print("{} appeared {} times in Cat_Layer.".format(name, number))

#cat_1 appeared 56 times in Cat_Layer.
#...
#cat_25 appeared 572 times in Cat_Layer.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 15:07:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1046981#M60783</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-04-14T15:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1047016#M60785</link>
      <description>&lt;P&gt;Do you have other CatName values in Cat_Layer that aren't in cat_names? In other words, do you want to count all occurrences of every&amp;nbsp;CatName value or only the ones you explicitly define?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 15:23:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1047016#M60785</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-04-14T15:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in loop</title>
      <link>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1047415#M60799</link>
      <description>&lt;P&gt;Good question. In that case, you would run this before:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;cat_names = [c[0] for c in arcpy.da.SearchCursor("Cat_Layer", ["CatName"])]
cat_names = list(set(cat_names))  # names of all cats in Cat_Layer&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 05:38:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/counting-in-loop/m-p/1047415#M60799</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-04-15T05:38:55Z</dc:date>
    </item>
  </channel>
</rss>

