Make a copy of your point feature class. The script below will add two new fields to one of them for Distance and Nearest_FID. Code is commented. Change paths to suit your needs.
import arcpy
# return unique values from field
# courtesy of arcpy cafe at https://arcpy.wordpress.com/2012/02/01/create-a-list-of-unique-field-values/
def unique_values(table, field):
with arcpy.da.SearchCursor(table, [field]) as cursor:
return sorted({row[0] for row in cursor})
# workspace where the two point files reside
arcpy.env.workspace = r"C:\Users\*****\Documents\ArcGIS\Default.gdb"
# reference to each point set
pointset_1 = "pointset_1"
pointset_2 = "pointset_2"
# add fields to one of them
arcpy.AddField_management(pointset_1, "Distance", "DOUBLE")
arcpy.AddField_management(pointset_1, "Nearest_FID", "LONG")
# create list of unique group values
group_names = unique_values(pointset_1, "NAME")
# for each group
for group in group_names:
query_1 = "NAME = '" + group + "'"
query_2 = "NAME <> '" + group + "'"
temp_lyr_1 = "lyr1_" + group
temp_lyr_2 = "lyr2_" + group
# select the group from pointset_1
arcpy.MakeFeatureLayer_management(pointset_1, temp_lyr_1)
arcpy.SelectLayerByAttribute_management(temp_lyr_1, "NEW_SELECTION", query_1)
# get all the values for OBJECTID in this group
fc_id_list = unique_values(temp_lyr_1, "OBJECTID")
# select all in pointset_2 that is not in the group
arcpy.MakeFeatureLayer_management(pointset_2, temp_lyr_2)
arcpy.SelectLayerByAttribute_management(temp_lyr_2, "NEW_SELECTION", query_2)
# for each feature in the group (pointset_1)
for fc_id in fc_id_list:
query_1 = "NAME = '" + group + "' AND OBJECTID = " + str(fc_id)
temp_lyr_3 = "lyr_" + str(fc_id)
# select only that feature
arcpy.MakeFeatureLayer_management(temp_lyr_1, temp_lyr_3)
arcpy.SelectLayerByAttribute_management(temp_lyr_3, "NEW_SELECTION", query_1)
# perform near analysis for that feature to the other groups in pointset_2
arcpy.Near_analysis(temp_lyr_3, temp_lyr_2)
# according to ArcGIS docs this will automatically set non selected each time to -1
# so we grab the data and insert it into our created fields.
with arcpy.da.SearchCursor(temp_lyr_3, ["NEAR_DIST", "Distance", "NEAR_FID"]) as cursor:
for row in cursor:
arcpy.CalculateField_management(temp_lyr_3, "Distance", row[0], "PYTHON_9.3")
arcpy.CalculateField_management(temp_lyr_3, "Nearest_FID", row[2], "PYTHON_9.3")
# you can write more to remove unwanted fields NEAR_DIST, NEAR_FID etc