Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

For example, if I have 100 records, and I want to sample 10% of the records. How do I tell the search cursor in arc10 to read the value of the 9th record, or every 10. I tried to do row[0 + count] and updated a count variable += 9 but it said I can't slice a row. Any suggestions, thanks.

share|improve this question
    
Do you want a random sample? If so check out Hawth's Tools: spatialecology.com/htools/rndsel.php –  blah238 Oct 17 '13 at 18:11

2 Answers 2

You could avoid the counter variable by using enumerate and modulus operator:

import arcpy
f = r'C:\esri-extract-data-where\test_data\test_data.gdb\export_test_1'

cursor = arcpy.SearchCursor(f)
for i,r in enumerate(cursor):
    if i % 10 == 0:
        print i,r
del cursor

or in arcpy 10.1+

with arcpy.da.SearchCursor(f, field_names='*') as cursor:
    for i,r in enumerate(cursor):
        if i % 10 == 0:
            print i,r
share|improve this answer

You can add a counter to your loop, so before you do the read line set your counter +=1, then put your read line into an if statement psedo code ex.

counter = 0
for i in list:
    counter +=1
    if counter ==9:
        read
        counter=0
share|improve this answer
    
if I did counter == 9, wouldn't that only run the code after the "if" statement one time, and not on 19,29,39, etc ? –  Brett Oct 17 '13 at 16:42
    
True, revised to correct that issue. –  Hotpepper Oct 17 '13 at 17:03
    
The revision doesn't seem to work either. It is running the code 1 time on the 9th record and then stops. Wouldn't I need to re-set the counter to 0 for the code to repeat? –  Brett Oct 17 '13 at 17:08
    
Was a typo-revised. –  Hotpepper Oct 17 '13 at 17:14
    
Thanks, that worked. –  Brett Oct 17 '13 at 17:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.