# iterating and exporting all combinations of features as shapefiles

I am trying to export individual shapefiles of all 2 pair combinations from a multi-feature polygon shapefile.

To explain this better as set of FID's 0, 1, 2, 3, 4 would be exported as individual shapefiles of FID pairs:

('0', '1'), ('0', '2'), ('0', '3'), ('0', '4'), ('1', '2'), ('1', '3'), ('1', '4'), ('2', '3'), ('2', '4'), ('3', '4')

Im not so good with python so my attempts with intertools.combination etc have not worked out. Any help would be appreciated. Thanks for any advice and help.

-
You tried list(itertools.combinations(range(5), 2))? –  Mike T Apr 12 '12 at 23:41

As @MikeToews suggests, using itertools.combinations you can get the list of combinations based on the FID, so you'd have a script that looks something like:

import arcpy
import itertools

arcpy.env.workspace = r"C:\path\to\workspace"

base_shapefile = "some_shapefile.shp"

oid_field = arcpy.Describe(base_shapefile).OIDFieldName
feature_count = int(arcpy.GetCount_management(base_shapefile).getOutput(0))

for (i, j) in itertools.combinations(range(feature_count), 2):
arcpy.Select_analysis(base_shapefile , "combination_%i_%i.shp" % (i, j), '"%s" in (%i, %i)' % (oid_field, i, j))

-
Note that this will only work for shapefiles, if it's in a geodatabase then it gets an OBJECTID which are not necessarily sequential (e.g. if you delete a feature). In that case you would need to create a list of all the OIDs and run that through combinations. –  blah238 Apr 13 '12 at 2:34
Thanks @om_henners this works great. I have learned a lot from this. To make it work in my environment I did add a arcpy.env.workspace = r"S:\folder_path" statement at the front of the script to specify where the new shapefiles can be stored. –  user6784 Apr 13 '12 at 17:27
No worries @user6784 - modified the script so that workspace is there. –  om_henners Apr 14 '12 at 10:20