Select to view content in your preferred language

Intersecting Lists Problem

2238
14
Jump to solution
03-19-2019 03:08 PM
AlanDodson
New Contributor III

First post here, please be gentle...

I use ArcMap 10.3.1 and I'm having trouble with part of a Python script which will always be run from the Current mxd and active data frame.

I have to pass a list of layers to a function. I have given the script a list of all potential layers for this function (List 1).  Some of those potential layers will actually be in my active data frame (always at least 1), along with other layers that are not compatible with the next function (List2).  I want to only work with the potential layers that are actually present in my active data frame (List3)

I've tried the following:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

df = mxd.ActiveDataFrame

List 1 = ["September", "October", "November", "December"]

List 2 = arcpy.mapping.ListLayers(df)  # layers are named September and December

List 3 = list(set.(List1).intersection(List2))

The result is an empty list when it should be September, December.

I think the syntax roughly works, but the problem is that List2 returns something similar to <layer u'September'> so the two layers with identical names are not recognized as such.  

(For context, I'll then be using List3 to print unique messages depending on the layer name returned... In case this helps)

Can anyone help me out? 

Tags (2)
0 Kudos
14 Replies
JoshuaBixby
MVP Esteemed Contributor

Since lyr.name returns a unicode,

>>> type(lyr.name) 
<type 'unicode'>
>>> 

I am interested in what you are doing that requires conversion to str, since Unicode and byte strings can be compared directly.

>>> u = u"September"
>>> type(u)
<type 'unicode'>
>>> 
>>> s = "September"
>>> type(s)
<type 'str'>
>>> 
>>> u == s
True
>>> 
AlanDodson
New Contributor III

I am a bit lost to be honest!  I'd love to explain but at the moment I don't fully understand what my script is doing and how I can go from there.  Perhaps this problem stems from me typing in the first list and hard-coding that into the script, along with each of the subsets of that list.  The only real arcpy listlayers list is the one I get from my active data frame.  I couldn't figure out any other way to do it - especially when I have to make the subset lists to weed out features.

To recap, I have a list of all potential layer names which I have hardcoded like  List1 = ["Quarries", "Ponds", "Gullies", "Runways"]. 

Not all of these layers are present in each map, so I want to run a function with only the layers from (List1) that are actually in my map (List3 - Ponds & Runways). I have other layers in my dataframe that I cannot fun the function on because the result is not needed and/or I cannot predict what will happen if other users add other datatypes to the dataframe.  Every layer in the dataframe makes up List2 (Features, Annotation, Grids, Rasters, user-specific layers etc). 

So using the snippet you shared with me I was able to weed out the unnecessary layers, but if I try to print List3 as a check nothing happens, and if I try to get the selection set after my Select By Location loop runs (referenced in another post you have graciously replied to) nothing happens - the script completes properly with no errors, but I do not get the result that I am looking for, 'getSelectionSet > 0' and FIDSet != '' queries do not return anything.  Because they do not return anything I can't pass the names of the layers which contain a selection to a bunch of if / elif statements to output specific instructions if that layer meets my conditions...

I hope to get a handle on things with our on-site ESRI help but he is stretched thin these days.

I do appreciate your help though, it's made things better   And I'm learning.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It appears you want a list of layers based on a predefined list of layer names.  Does this work for you:

>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> df = mxd.activeDataFrame
>>> List1 = ["September", "October", "November", "December"]
>>> List3 = [lyr for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df) if lyr.name in List1]
>>> List3
[<map layer u'December'>, <map layer u'September'>]
>>> 
AlanDodson
New Contributor III

If I'm reading that right - List1 is typed-in, hardcoded, predefined etc; List3 creates a true list of layer objects based whether or not the layer object name of everything in the dataframe (pseudo-List2) appears in List1;  List3 therefore becomes the resulting list of true layer objects that can be recognized by getSelectionSet and any other method that recognizes layers...  If I'm interpreting that correctly I think it would be exactly right.....  I'll have to wait to try it out in the morning but this looks promising...  Thank you again for walking me through this!  I'll let you know how it turns out  

0 Kudos
AlanDodson
New Contributor III

This works even better for my purposes than the first message I marked as correct!  Thank you, you've unlocked the key to my problem and I'm happily on my way now....  Until the next inexperienced coder hiccup!  Cheers.

0 Kudos