# Counting in loop

307
6
Jump to solution
04-14-2021 05:57 AM by
New Contributor

Good day everybody,

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.

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)

Could you please recommend to me how to use the loop to count the number of each cat in my field?

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)).

1 Solution

Accepted Solutions Occasional Contributor III

``````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.``````

Have a great day!
Johannes
6 Replies by MVP Regular Contributor

You can use a default dictionary for this!

``````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``````

Output:

defaultdict(<type 'int'>, {'nine': 1, 'five': 1, 'three': 1, 'two': 2, 'one': 3}) by MVP Regular Contributor

Can you elaborate a bit on this? Do you mean you want the count of occurrences per cat, something like:

`cat 1: 100cat 2: 2500etc...`

I think part of the problem here is that the myList object is being redefined as an empty list for every iteration.

- Josh Carlson
Kendall County GIS by MVP Esteemed Contributor

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. Occasional Contributor III

``````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.``````

Have a great day!
Johannes by MVP Regular Contributor

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 CatName value or only the ones you explicitly define? Occasional Contributor III

Good question. In that case, you would run this before:

``````cat_names = [c for c in arcpy.da.SearchCursor("Cat_Layer", ["CatName"])]
cat_names = list(set(cat_names))  # names of all cats in Cat_Layer``````

Have a great day!
Johannes
{"ft_lang_label":"__label__en","ft_lang_prob":0.8118193,"math_prob":0.7633845,"size":2950,"snap":"2021-21-2021-25","text_gpt3_token_len":790,"char_repetition_ratio":0.12627292,"word_repetition_ratio":0.21794872,"special_character_ratio":0.2979661,"punctuation_ratio":0.17657046,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95470154,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,5,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-23T07:03:50Z\",\"WARC-Record-ID\":\"<urn:uuid:f528de66-1f3f-4c43-a45a-884de971ea2c>\",\"Content-Length\":\"294800\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fdf65153-9b56-4432-aa3e-0848900dd7f4>\",\"WARC-Concurrent-To\":\"<urn:uuid:9532650d-5f55-45a8-b12c-5126ce47aea8>\",\"WARC-IP-Address\":\"13.32.182.99\",\"WARC-Target-URI\":\"https://community.esri.com/t5/python-questions/counting-in-loop/td-p/1046863\",\"WARC-Payload-Digest\":\"sha1:WWEAPRCZ6RO7LNLHMJFPMNQ527H7B6NK\",\"WARC-Block-Digest\":\"sha1:STYURK6CPCJLOWNX6HGIY7KQXK2HS3IX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488534413.81_warc_CC-MAIN-20210623042426-20210623072426-00558.warc.gz\"}"}