Unable to get the difference between 2 lists

93 views
Skip to first unread message

likage

unread,
Nov 21, 2014, 2:40:26 AM11/21/14
to python_in...@googlegroups.com
I am trying to find the difference between two lists - charLs and rigLs but as I tried to use the list and set methods as I found online, it is giving me very different results.

In the example below, I should be seeing female01 and elderly01 in the diff1 results but yet I am getting male01
Even as I switched around the set, like in diff2, this time round it is giving the list of the 3 rigs that was in the scene.

Am I doing anywhere wrong or using the wrong code commands?


pubObj
= cmds.ls(type = "publishedRef")
rigLs
= []
for item in pubObj:
    nameSplit
= item.split(':')
    rigLs
.append(nameSplit[0])

grpNode
= cmds.ls("SceneGrp", type=grpNode.name())
check
= cmds.objExists("|SceneGrp|characterRig")
charLs
= cmds.listRelatives("|SceneGrp|characterRig", c=True)

# Assumingly the following are the items appended into the list
# rigLs : [u'male01', u'female01', u'elderly01']
# charLs :[u'male01']

diff1
= list(set(charLs)-set(rigLs))
# Results : [u'male01']

diff2
= list(set(rigLs)-set(charLs))
# Results : [u'male01', u'female01', u'elderly01']


Mark Jackson

unread,
Nov 21, 2014, 3:00:41 AM11/21/14
to python_inside_maya
Think what you're probably after is the difference call in sets, ie, return the difference between the two sets

diff2 = list(set(rigLs)^set(charLs))

What the minus sign does is a return elements in the first arg that aren't in the second, where as the ^ sign returns the actual difference. That said with your lists I didn't get your results?

hope that helps

Mark

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/35777d5c-62d6-4341-880b-9f3e5bd123e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-------------------------------------
Mark Jackson
CEO / Technical Director
red9consultancy.com

likage

unread,
Nov 21, 2014, 3:14:12 AM11/21/14
to python_in...@googlegroups.com
Hey Mark,

Thanks for getting back to me. I am still not getting the results, seems to be the unicode error as I am appending the items into list.
It works unless i took out the u in each of the list.. Is there any ways that I can take out the unicode or do I need to do some more splittings?

Justin Israel

unread,
Nov 21, 2014, 3:20:23 AM11/21/14
to python_in...@googlegroups.com
I'm not getting any issues getting the different even with sets of mixed unicode and ascii objects. Can you give a specific example that can reproduce your error?

Also this can be a little bit easier to read:

a = set([u'male01', u'female01', u'elderly01'])
b = set([u'male01'])
a.difference(b)
# {u'elderly01', u'female01'}

The operators are defined to map the methods of sets like union, difference, intersection, ...



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Marcus Ottosson

unread,
Nov 21, 2014, 3:20:35 AM11/21/14
to python_in...@googlegroups.com

If you can post an example where you show us the actual content of the two list you are comparing, it will be much easier to help you find what might be wrong.

Something like:

a = {1, 2, 3}
b = {2, 3, 4}
print a - b  # Remove items from a that exist in b
# 1
print b - a  # Remove items in b that exists in a
# 4
print a ^ b  # Symmetrical difference between a and b
# [1, 4]

All the operations can be found here:
https://docs.python.org/2/library/sets.html#set-objects


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Marcus Ottosson

unread,
Nov 21, 2014, 3:21:04 AM11/21/14
to python_in...@googlegroups.com

likage

unread,
Nov 21, 2014, 3:35:36 AM11/21/14
to python_in...@googlegroups.com
I took back what I mentioned about the unicode. It is somewhat hard for me to give an example, as publishedRef is an in-house module.
Tried out the method supplied by Justin, it is still printing [u'male01', u'female01', u'elderly01'] which is the same results as diff2 - in my first post...

I am basically trying to capture and compare the names obtained in both rigLs and charLs

Justin Israel

unread,
Nov 21, 2014, 3:41:53 AM11/21/14
to python_in...@googlegroups.com
Forget the Maya specific parts. If it is related to the sets then you should be able to reproduce it with generic Python. If you can't, then it is some other mistake or mishandling going on in your code.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Nov 21, 2014, 4:17:15 AM11/21/14
to python_in...@googlegroups.com
By doing a simple set example using generic Python, it is working with no issues. I am achieving the results that I had wanted see.

So I guess this may be something due to the Maya handling part.. I thought if appending the name, it will not affect anything much

likage

unread,
Nov 21, 2014, 4:41:17 AM11/21/14
to python_in...@googlegroups.com
Adding on, seems to be an issue around the listRelatives part in my case

Eduardo Grana

unread,
Nov 21, 2014, 7:50:14 AM11/21/14
to python_in...@googlegroups.com
Hey Mark,
Be aware that when you do set(list) you discard the duplicates on the list, so you may be comparing different things...
by the way, aren't sets deprecated?
Cheers!
Eduardo


For more options, visit https://groups.google.com/d/optout.



--

Marcus Ottosson

unread,
Nov 21, 2014, 9:09:36 AM11/21/14
to python_in...@googlegroups.com

aren’t sets deprecated?

Certainly not! You may be thinking of the top of the link I posted above as reference. That is in regards to the “sets” module, which looks to have been merged into top-level Python functionality. Quite the opposite of deprecation. :)



For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Eduardo Grana

unread,
Nov 21, 2014, 10:06:51 AM11/21/14
to python_in...@googlegroups.com
Sorry, my bad...
Don't remember quite well why when i used it in a previous python version it gave me
a deprecation warning...


For more options, visit https://groups.google.com/d/optout.



--

Paul Molodowitch

unread,
Nov 21, 2014, 11:04:03 AM11/21/14
to python_inside_maya
Question - are your results that are inside of rigLs truly the direct result of a call to cmds.listRelatives()?  Are you sure that the objects inside of rigLs are truly strings (or unicode)?

I ask because I've noticed that in maya 2015, a repr was added to MObjects... which would be nice, except that the definition really would have been more appropriate for __str__, not __repr__, as it can make it hard to tell that you are working with MObjects, and not just strings.

To see what I mean, try running this in Maya 2015*:


import maya.cmds as cmds

# set up the indicated hierarchy
cmds.createNode('transform', name='male01')
cmds.group(name='characterRig')
cmds.group(name='SceneGrp')

# and set up rigLs / charLs as indicated
rigLs = [u'male01', u'female01', u'elderly01']
parent = '|SceneGrp|characterRig'
charLs = cmds.listRelatives(parent, c=True)
print charLs
# [u'male01']

# this works as expected...
diff1 = list(set(charLs)-set(rigLs))
print diff1
# []
diff2 = list(set(rigLs)-set(charLs))
print diff2
# [u'female01', u'elderly01']

# but if you were to somehow end up with MObjects, the results may seem confusing

def apiGetChildren(parent):
    # you can ignore the inner workings of this, just know that it will act like
    # cmds.listRelatives(parent, c=True), except return api MObjects
    import maya.OpenMaya as om
    sel = om.MSelectionList()
    sel.add(parent)
    parentObj = om.MObject()    
    sel.getDependNode(0, parentObj)
    parentMFn = om.MFnDagNode(parentObj)
    return [parentMFn.child(i) for i in xrange(parentMFn.childCount())]

charLs = apiGetChildren(parent)

# in maya2015, charLs now SEEMS to be the same as before...
print charLs
# [u'male01']

# ...but you'll get unexpected results...
diff1 = list(set(charLs)-set(rigLs))
print diff1
# [u'male01']
diff2 = list(set(rigLs)-set(charLs))
print diff2
# [u'male01', u'female01', u'elderly01']
If you think something like this is going on, you can do this:

print charLs
print [type(x) for x in charLs]
print rigLs
print [type(x) for x in rigLs]
...and let us know what you get.

- Paul

*Note, I wasn't actually able to test this in Maya 2015, but it should act like this from what I recall. Not 100% sure that the MObject.__repr__ returns unicode, as opposed to just str, though...
Reply all
Reply to author
Forward
0 new messages