Set Function with addtional field printed

3790
4
Jump to solution
02-09-2015 08:49 AM
LarryAdgate
Occasional Contributor

Some help on this please, Thanks

Script work works great for finding Facilityid numbers not found in “b” or final =a-b. But to make the script more useful an additional field or
“NAME” should be printed in conjunction with the Facilityid number or “FACILITYID & NAME” and this is where I need some help. The “NAME” field contains text (ex:“Redondo Tank”) and is difficult to add or subtract and the set function works perfect for my task.

Thanks,

Larry Adgate    

# Import arcpy module
import arcpy

# Local variables:
Index = "C:\\Projects\\UpdatePlantFacilityIndex.mdb\\FacilityIndex2013"
Arden_ns = "C:\\Projects\\UpdatePlantFacilityIndex.mdb\\UpdatePlantFacilityIndex\\Network"

a = set(r[0] for r in arcpy.da.SearchCursor(Index, ["FACILITYID"]))
a1 = set(r[0] for r in arcpy.da.SearchCursor(Index, ["NAME"])) #Addition field data to accompany FacilityID
b = set(r[0] for r in arcpy.da.SearchCursor(Arden_ns, ["FacilityID"]))

final = a+a1-b

print "Arden System Discrepency: {0},".format(", ".join(sorted(final)))

#error Message:    final = a+a1-b
#error message: TypeError: unsupported operand type(s) for +: 'set' and 'set'

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You could try this:

import arcpy
Index = "C:\\Projects\\UpdatePlantFacilityIndex.mdb\\FacilityIndex2013"
Arden_ns = "C:\\Projects\\UpdatePlantFacilityIndex.mdb\\UpdatePlantFacilityIndex\\Network"

# list of facility ID's in Arden_ns
b = set(r[0] for r in arcpy.da.SearchCursor(Arden_ns, ["FacilityID"]))

# list facilities not in arden_ns
flds= ("FACILITYID", "NAME")
dct = {r[0]: r[1] for r in arcpy.da.SearchCursor(Index, flds) if not r[0] in b}

# print list
for fac_id, name in sorted(dct.items()):
    print "{0}: {1}".format(fac_id, name)

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

Do the FacilityID and NAME fields contain the same information? In my post Some Python Snippets you can see how to work with sets and lists:

# Compare two lists and get the items in list 1 but not in list 2
listDifference = list(set(myList1) - set(myList2))

# Compare two lists and get the items that are in both lists
listSame = list(set(myList1) & set(myList2))

# join or merge two sets
new_set = first_set.union(other_set)
# or:
new_set = set(list(first_set) + list(other_set))
LarryAdgate
Occasional Contributor

Xander Thanks for your Help

The " FacilityId" Field will contains numbers such as NS1025, NS1008, NS1010, CS1002

The "NAME" Field will contain text data such as- Redondo Tank, Sludge Tank, Pacific Tank

A Typical Print out would say:   NS1025  Redondo Tank

                                                  NS1008  Sludge Tank

                                                  NS1010  Pacific Tank

                                                              

Larry

0 Kudos
XanderBakker
Esri Esteemed Contributor

You could try this:

import arcpy
Index = "C:\\Projects\\UpdatePlantFacilityIndex.mdb\\FacilityIndex2013"
Arden_ns = "C:\\Projects\\UpdatePlantFacilityIndex.mdb\\UpdatePlantFacilityIndex\\Network"

# list of facility ID's in Arden_ns
b = set(r[0] for r in arcpy.da.SearchCursor(Arden_ns, ["FacilityID"]))

# list facilities not in arden_ns
flds= ("FACILITYID", "NAME")
dct = {r[0]: r[1] for r in arcpy.da.SearchCursor(Index, flds) if not r[0] in b}

# print list
for fac_id, name in sorted(dct.items()):
    print "{0}: {1}".format(fac_id, name)
LarryAdgate
Occasional Contributor

Thanks Xander,  the script worked great. My plans were to run  the Index againt 38 other feature classes and this will give me food for thought. Thanks, Larry 

0 Kudos