Select to view content in your preferred language

ArcPy equivelant to set([list])

2418
7
05-08-2012 08:14 AM
StephenKruzik
Deactivated User
I have a python script that works fine as a stand-alone python script.  In an effort to integrate it as a tool in ArcGIS however, it's not working.  After some research, I came across a forum thread that stated the python variable type 'set' is not usable in Arcpy.

Is there an alternative to make this work?  Some way of expressing set(
    ) in arcpy?


Basically I have a feature class with features that intersect a selected point.  Some of these features have duplicate names.  the script is designed to take the names out of the intersecting features, and append them into a 'name' field in the point feature class.  To avoid having duplicate data names, I used set(
    ) in Python to go through the list of features that are intersecting, and weed out the duplicates.  So eg, my list originally was list = [a,b,c,f,h,a,c,c,c,t].  Using set(list), it becomes [a,b,c,f,h,t].  I then convert it to string, make it look all fancy and nice, and throw it into the 'name' field of my point featureclass.


any help is appreciated.
Tags (2)
0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Stephen,

Try one of the following:

List = ['a','b','c','f','h','a','c','c','c','t']
List = dict.fromkeys(List)
List = List.keys()
print List


or

List = ['a','b','c','f','h','a','c','c','c','t']
outlist = []
for element in List:
    if element not in outlist:
        outlist.append(element)
print outlist
StephenKruzik
Deactivated User
it worked.
thanks.
0 Kudos
PhilMorefield
Frequent Contributor
I'm a little confused by this question. In what way are Python set objects incompatible with arcpy?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I'm a little confused by this question. In what way are Python set objects incompatible with arcpy?


Hi Phil,

When returning a set object from a list I run into the problem of receiving "set('value')" instead of simply 'value'.  Ex:

List = ['a','b','c','f','h','a','c','c','c','t']
List = dict.fromkeys(List)
List = List.keys()
print List[0]


Will return: a

List = ['a','b','c','f','h','a','c','c','c','t']
print set(List[0])


Will return: set(['a'])
0 Kudos
PhilMorefield
Frequent Contributor
Will return: set(['a'])

Only because everything is wrapped in the set() Python class.
>>>myList = list(set(['a','b','c','f','h','a','c','c','c','t']))
>>>myList[0]
'a'
>>>for item in set(['a','b','c','f','h','a','c','c','c','t']):
...       print item
a
c
b
f
h
t

An important point is that set() objects are unordered, so if order is important one should also wrap everything in sorted(). Please let me know if I'm not understanding the issue correctly.
0 Kudos
StephenKruzik
Deactivated User
if you're running directly from a python script independant of ArcGIS, then yes, it works just fine.  That was actually my problem.  I was running my script as a stand-alone using the IDLE window to process everything.  I got tired of having to navigate to the file everytime though, so I tried to add it as a tool within Arctoolbox.

If you process it from the Arcpy window within ArcMap, or as a tool script that requires much closer interaction with ArcGIS, then set(
    ) will not work.


Open up the Python window within ArcMap and try it.  That's probably the best way to see it.
0 Kudos
PhilMorefield
Frequent Contributor
if you're running directly from a python script independant of ArcGIS, then yes, it works just fine.  That was actually my problem.  I was running my script as a stand-alone using the IDLE window to process everything.  I got tired of having to navigate to the file everytime though, so I tried to add it as a tool within Arctoolbox.

If you process it from the Arcpy window within ArcMap, or as a tool script that requires much closer interaction with ArcGIS, then set(
    ) will not work.

    Open up the Python window within ArcMap and try it.  That's probably the best way to see it.
    I'm still not seeing any errors. The set() class is working normally for me in the ArcMap Python window (see attached). I'm using AGD 10, service pack 4, with the spatial join patch.

    Are you importing any other libraries like this:
    from (some Python library) import *

    The only thing I can think of is the name space might be polluted and you're calling a different set function or class.

    You mentioned a thread in a forum that discussed this issue. Do you still have a link to that? A screenshot of the error would be helpful too.
0 Kudos