How do I add a list to arcpy.da.SearchCursor?

1681
6
Jump to solution
05-18-2018 07:47 AM
JordanMiller4
Occasional Contributor III

How can I use systemList variable with several defined types with arcpy.da.SearchCursor? Currently, I have it looking at '009' but I would like it to find every system defined in systemList.

import arcpy

fc = 'Database Connections\SDE.sde\NEO_1.SDE.P_PipeSystem\NEO_1.SDE.P_Meters'
field = 'SYSTEMNUMBER'

systemList = ["009", "10", "12", "13", "21", "22", "27", "27A", "27B", "27C", "30", "31", "33", "34", "35", "36"]


cursor = arcpy.da.SearchCursor(fc, (field, "SYSTEMNUMBER"), """SYSTEMNUMBER = '009'""")
count = 0 

# For each row print the 
for row in cursor:
    # Print the name of the gas systems
    #
    #print(row[1])
    #result = arcpy.GetCount_management(fc)
    #print('{} has {} records'.format(row[1], result[0]))
    count += 1  

print('{} has {} records'.format(row[1],count))  

0 Kudos
1 Solution

Accepted Solutions
SanghongYoo1
New Contributor II

If I understand your problem, I think you just need unique field value counts on SYSTEMUNUMBERS. I got the code from here and modified a little.

import arcpy

table = 'Database Connections\SDE.sde\NEO_1.SDE.P_PipeSystem\NEO_1.SDE.P_Meters'
field = 'SYSTEMNUMBER'

with arcpy.da.SearchCursor(table, field) as cursor:
    occurances = [row[0] for row in cursor]
for i in sorted(set(occurances)):
    icount = occurances.count(i)
    print("{} has {} records".format(i, icount))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

So the field name is systemList and you just want to have the cursor select records with values that are in the list?

For your search cursor, why are you passing both field and "SYSTEMNUMBER"?  Since field is defined as "SYSTEMNUMBER", you are asking the cursor for return the same field twice, which is unusual. 

0 Kudos
JoeBorgione
MVP Esteemed Contributor

I'm not sure what your objective is, but I have used something like this in the past:

 types = "('AVE','BAY','BLVD','CIR','CT','CV','CYN','DR','EXPY','FWY','HWY','LN','PKWY','PL','RD','ROW','ST','SQ','TER','WY','')"

#.......

if theType in types:
    do something
else:
    do something else
That should just about do it....
0 Kudos
JordanMiller4
Occasional Contributor III

In our meter layer we have a field called SYSTEMNUMBERS. I was asked if it was possible to write a python script that could count each meter record in each system and display the total amount. We have over 70 systems. The script is able to display only 1 system which is the 009. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You might be making this more difficult than it needs to be.  Have you thought about using Summary Statistics—Help | ArcGIS Desktop and summarizing by SYSTEMNUMBERS and then deleting the results for the system numbers you are not interested in?  If this is going to be a regular activity, it might be worth scripting.  If this only happens occasionally, it might be easier to run a GP tool and manually clean up the results table.

If there is some objection to using Summary Statistics, there are two ways to handle this using cursors.  One is to pass a SQL WHERE clause to the cursor to limit the records and the other is retrieve all the records and use Python to do the following, which is what Joe is proposing.

SanghongYoo1
New Contributor II

If I understand your problem, I think you just need unique field value counts on SYSTEMUNUMBERS. I got the code from here and modified a little.

import arcpy

table = 'Database Connections\SDE.sde\NEO_1.SDE.P_PipeSystem\NEO_1.SDE.P_Meters'
field = 'SYSTEMNUMBER'

with arcpy.da.SearchCursor(table, field) as cursor:
    occurances = [row[0] for row in cursor]
for i in sorted(set(occurances)):
    icount = occurances.count(i)
    print("{} has {} records".format(i, icount))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JordanMiller4
Occasional Contributor III

Thanks, this was what I was trying to achieve!

0 Kudos