Solved! Go to Solution.
Hello Jacob,
Python is awesome. It can be tricky to learn, but it's great once you do. In the future, it would be a lot easier if you posted the python code as text in your posting (see here for instructions).
I've made your requested changes to the script and tried to add lots of comments (text after "#" are comments) to help you understand whats going on:
#import modules you'll need in your script:
import arcpy
import random
from arcpy import env
#allow outputs to be overwritten
#(so you don't have to delete them before each time you run
env.overwriteoutput = True
#set input variables:
inputfc = "C:/EsriPress/Python/Date/Exercise13/points.shp"
outputic = "C:/EsriPress/Python/Data/Exercise13/Results/random.shp"
pctRecords = 50
#empty lists for later use:
inlist = []
randomlist = []
#get the unique id field for the input feature class
desc = arcpy.Describe(inputfc)
fldname = desc.OIDFieldName
#iterate through the input feature class (see below for an alternative to the while loop)
#get the rows for the input feature class (this is similar to the attribute table)
rows = arcpy.SearchCursor(inputfc)
row = rows.next() #get the next single row (in this case, it's the first row)
count=0 #set a counter to 0
#while row exists, do some stuff:
while row:
id = row.getValue(fldname) #get the id from the current row
inlist.append(id) #add that id to a list we created earlier
count+=1 #add one to our counter
row = rows.next() #switch to the next row
#Now that we have the count we can set our outcount
#first turn input percent to a deciman (example 0.5 instead of 50),
#then times it by the count
outcount = (pctRecords*0.01)*count
while len(randomlist) < outcount:
#while the randomlist is smaller than the desired outcount:
selitem = random.choice(inlist)
#pick a random item from the list we created in the previous while loop
randomlist.append(selitem) #add that randomly picked id to the random list
inlist.remove(selitem) #remove that id from the list so it won't randomly get picked again
#create a sql statement to do a select by attribute:
sqlexp = '"' + fldname + '"' + ' in ' + '(' + str(randomlist)[1:-1] + ')'
#make a selection by selecting all the id's in the random list from the original file
arcpy.MakeFeatureLayer_management(inputfc, "selection", sqlexp)
#turn that selection into a shapefile:
arcpy.CopyFeatures_management("selection", outputfc)
print "Script is complete!"
As an alternative to your first while loop, I would prefer to use a for loop (but either will work). This is what the for loop would loop like:
#iterate through the input feature class (alternative to the while loop) rows = arcpy.SearchCursor(inputfc) #get the rows for the input feature class (this is similar to the attribute table) count=0 #set a counter to 0 for row in rows: #goes through each row in rows and does stuff with that row id = row.getValue(fldname) #get the id from the current row inlist.append(id) #add that id to a list we created earlier count+=1 #add one to our counter
Let me know if you have any questions. Good luck!
Hello Jacob,
Python is awesome. It can be tricky to learn, but it's great once you do. In the future, it would be a lot easier if you posted the python code as text in your posting (see here for instructions).
I've made your requested changes to the script and tried to add lots of comments (text after "#" are comments) to help you understand whats going on:
#import modules you'll need in your script:
import arcpy
import random
from arcpy import env
#allow outputs to be overwritten
#(so you don't have to delete them before each time you run
env.overwriteoutput = True
#set input variables:
inputfc = "C:/EsriPress/Python/Date/Exercise13/points.shp"
outputic = "C:/EsriPress/Python/Data/Exercise13/Results/random.shp"
pctRecords = 50
#empty lists for later use:
inlist = []
randomlist = []
#get the unique id field for the input feature class
desc = arcpy.Describe(inputfc)
fldname = desc.OIDFieldName
#iterate through the input feature class (see below for an alternative to the while loop)
#get the rows for the input feature class (this is similar to the attribute table)
rows = arcpy.SearchCursor(inputfc)
row = rows.next() #get the next single row (in this case, it's the first row)
count=0 #set a counter to 0
#while row exists, do some stuff:
while row:
id = row.getValue(fldname) #get the id from the current row
inlist.append(id) #add that id to a list we created earlier
count+=1 #add one to our counter
row = rows.next() #switch to the next row
#Now that we have the count we can set our outcount
#first turn input percent to a deciman (example 0.5 instead of 50),
#then times it by the count
outcount = (pctRecords*0.01)*count
while len(randomlist) < outcount:
#while the randomlist is smaller than the desired outcount:
selitem = random.choice(inlist)
#pick a random item from the list we created in the previous while loop
randomlist.append(selitem) #add that randomly picked id to the random list
inlist.remove(selitem) #remove that id from the list so it won't randomly get picked again
#create a sql statement to do a select by attribute:
sqlexp = '"' + fldname + '"' + ' in ' + '(' + str(randomlist)[1:-1] + ')'
#make a selection by selecting all the id's in the random list from the original file
arcpy.MakeFeatureLayer_management(inputfc, "selection", sqlexp)
#turn that selection into a shapefile:
arcpy.CopyFeatures_management("selection", outputfc)
print "Script is complete!"
As an alternative to your first while loop, I would prefer to use a for loop (but either will work). This is what the for loop would loop like:
#iterate through the input feature class (alternative to the while loop) rows = arcpy.SearchCursor(inputfc) #get the rows for the input feature class (this is similar to the attribute table) count=0 #set a counter to 0 for row in rows: #goes through each row in rows and does stuff with that row id = row.getValue(fldname) #get the id from the current row inlist.append(id) #add that id to a list we created earlier count+=1 #add one to our counter
Let me know if you have any questions. Good luck!
How do i submit that you answered what i had asked to give you credit for it?
and to add to the comment about the forums, etc. There are many threads here on GeoNet that list different resources for helping to learn Python. I just did a search for "learn python" in Geonet.
note: "API for Python" is different than Python. The API is fairly new and would be good to learn, but would cause confusion when you are still learning Python.
Check out some of these threads for some tips on python:
Can anyone suggest a good learning python website?
https://community.esri.com/message/85013
(not in any particular order)
Hi Jacob,
Please, can you help me with the solution to this question. you can email me please, jucoy2k@hotmail.com