<?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: Random Selection of Ploygons from Current Selection in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481307#M37645</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the reply. I'm with you on steps 2-5, what I can't seem to figure out is how to extract the attribute data for the polygons such as object id into a python list that I can then randomly select values from. Seems easy enough but is eluding me in the arcpy documentation. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fred&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 18 Dec 2013 21:09:08 GMT</pubDate>
    <dc:creator>FredKellner1</dc:creator>
    <dc:date>2013-12-18T21:09:08Z</dc:date>
    <item>
      <title>Random Selection of Ploygons from Current Selection</title>
      <link>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481305#M37643</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my python script I am selecting by attribute, and after doing so I would like to make a random selection of 50 polygons from this selection. I am trying to do this in an efficient manner. I have tried a manner of different solutions, but these involve exporting files and slow for loops. Any suggestions for a fast and efficient way to perform this random selection. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fred&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Dec 2013 16:34:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481305#M37643</guid>
      <dc:creator>FredKellner1</dc:creator>
      <dc:date>2013-12-18T16:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Random Selection of Ploygons from Current Selection</title>
      <link>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481306#M37644</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello, &lt;BR /&gt;&lt;BR /&gt;In my python script I am selecting by attribute, and after doing so I would like to make a random selection of 50 polygons from this selection. I am trying to do this in an efficient manner. I have tried a manner of different solutions, but these involve exporting files and slow for loops. Any suggestions for a fast and efficient way to perform this random selection. &lt;BR /&gt;&lt;BR /&gt;Thanks &lt;BR /&gt;&lt;BR /&gt;Fred&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Off the top of my head I can think of something like the following working:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;OL&gt; Select the polygons.&lt;BR /&gt;&lt;LI&gt; Get the IDs of the selected Polygons.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt; Create a dictionary that has incremental keys (0 - (n - 1)) where n is the number of selected polygons (see arcpy.GetCount). Each key should reference a ploygon's ID.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt; Import random and use &lt;A href="http://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-unique-random-numbers"&gt;random.sample(range(n), 50)&lt;/A&gt; to generate a list of 50 unique random numbers.&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt; For each random number, use the dictionary to get a list of all the corresponding polygon IDs&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt; Now use select by attribute to get all the polygons. This would be done using something like [ID_FIELD] IN (&amp;lt;IDs&amp;gt;)&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;BR /&gt;&lt;SPAN&gt;The exact way you code it will depend a lot on what your data looks like. Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Dec 2013 17:09:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481306#M37644</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-12-18T17:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: Random Selection of Ploygons from Current Selection</title>
      <link>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481307#M37645</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the reply. I'm with you on steps 2-5, what I can't seem to figure out is how to extract the attribute data for the polygons such as object id into a python list that I can then randomly select values from. Seems easy enough but is eluding me in the arcpy documentation. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fred&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Dec 2013 21:09:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481307#M37645</guid>
      <dc:creator>FredKellner1</dc:creator>
      <dc:date>2013-12-18T21:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Random Selection of Ploygons from Current Selection</title>
      <link>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481308#M37646</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The easiest way to do this would be to use a &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000" rel="nofollow noopener noreferrer" target="_blank"&gt;Search Cursor&lt;/A&gt;&lt;SPAN&gt; on the feature or table layer you created (not the source datasets) that has the initial selection applied (although I haven't tried doing this before):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Cursors honor layer/table view definition queries and selections. The cursor object only contains the rows that would be used by any geoprocessing tool during an operation.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your code could look something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

selected = [str(r[0]) for r in arcpy.da.SearchCursor('Layer Name', ['SOURCE_ID_FIELD'])]
query = 'SOURCE_ID_FIELD IN (' + ', '.join(selected) + ')'

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note that the exact syntax is going to depend on how you defined your feature or table layer, and whether or not this is shapefile, fgdb etc. The str(r[0]) is important because join() expects a list of strings.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also if the IDs are text then...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
query = "SOURCE_ID_FIELD IN ('" + "', '".join(selected) + "')"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... should work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Tough to test any of this without your data, but hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:14:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481308#M37646</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-11T21:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Random Selection of Ploygons from Current Selection</title>
      <link>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481309#M37647</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Oops. I kind of forgot what it was you were trying to do. I was also wrong about how random.sample() works. The dictionary isn't necessary and you can get it all in one line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
randomly_selected =&amp;nbsp; random.sample([str(r[0]) for r in arcpy.da.SearchCursor('Layer Name', ['SOURCE_ID_FIELD'])], 50)

query = "SOURCE_ID_FIELD IN ('" + "', '".join(randomly_selected) + "')"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ramdom.sample() is pulling random values from the input list. When you use the query, you will get your 50 random features.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:14:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/random-selection-of-ploygons-from-current/m-p/481309#M37647</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-11T21:14:26Z</dc:date>
    </item>
  </channel>
</rss>

