Discussion:
[Maya-Python] Checking order of list items in 2 lists
kiteh
2018-10-10 19:32:41 UTC
Permalink
Hi everyone,

I have 2 list items which contains the same amount of items, and I am
trying to check the order of one list to another.

Eg.
# This is derived from maya.cmds
list01 = ['cleve', 'adam', 'yuno', 'pete']

# This is derived from dictionary keys in a way it is capturing the same
info from the scene but derived very differently
list02 = ['pete', 'yuno', 'cleve', 'adam']


I am unable to use `list01 = list02` or something similar, because both
lists are called from different applications and hence this question of
mine.
And the reason I had wanted to the order to be the same is so that the
application that uses `list02` will populate in the same hierarchical
manner as in `list01` (outliner).

And neither would I want to/ can use `sorted` as this will mess up my
order. What is the best way to go about this?

P.S: Sorry in advance if this sounds like a very noob question...
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e248c2b5-26f0-4268-8bf8-73be9a446e99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-10-10 20:36:08 UTC
Permalink
Post by kiteh
Hi everyone,
I have 2 list items which contains the same amount of items, and I am
trying to check the order of one list to another.
Eg.
# This is derived from maya.cmds
list01 = ['cleve', 'adam', 'yuno', 'pete']
# This is derived from dictionary keys in a way it is capturing the same
info from the scene but derived very differently
list02 = ['pete', 'yuno', 'cleve', 'adam']
I am unable to use `list01 = list02` or something similar, because both
lists are called from different applications and hence this question of
mine.
And the reason I had wanted to the order to be the same is so that the
application that uses `list02` will populate in the same hierarchical
manner as in `list01` (outliner).
And neither would I want to/ can use `sorted` as this will mess up my
order. What is the best way to go about this?
P.S: Sorry in advance if this sounds like a very noob question...
There is no need to apologize for the perceived value of your question.

Can you please expand on your goal? I am confused between the part where
you want to "check the order of one list to another" vs "unable to use
`list01 = list02`" vs "neither would I want to/ can use `sorted`".

Are you trying to check if list02 is the same order as list01 in terms of
True or False? If you hadn't said list01 and list02 are the same length,
then I would have thought you were trying to iterate list02 sorted by the
order of items in list01. But that doesn't make sense if they both contain
the same items. Testing "==" between the two lists would tell you if they
are sorted the same way or not. Testing sorted(list02) == list01 would tell
you if they contain the same items.

Maybe you can illustrate your expected behaviour with a little more detail?
Post by kiteh
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/e248c2b5-26f0-4268-8bf8-73be9a446e99%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/e248c2b5-26f0-4268-8bf8-73be9a446e99%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA20uE8sqDFrTC%2Bg4B6f6c%3D4BZfnZf-KJVAfeMZQw2kMAQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
kiteh
2018-10-10 21:31:24 UTC
Permalink
Hi Justin, sorry that I may have made my question confusing/ I was
babbling, typing away of the way I am trying to phrase my question.
Let me try again :)

1. Deriving the hierarchy from Outliner
Eg. This is the hierarchical level as seen in my Outliner

|-- base
|--|-- names
|--|--|-- cleve
|--|--|-- adam
|--|--|-- yuno
|--|--|-- pete


If I run a cmds command as follows, note that the result is as what I will
be seeing in the Outliner.

list01 = cmds.listRelatives('base', ad=True, f=True)[:-1]
print all_items
"""
Result :
['|base|name|cleve',
'|base|name|adam',
'|base|name|yuno',
'|base|name|pete']
"""


2. As mentioned, list02 is derived from dictionary keys from a custom
module, but the result/ order from the dictionary's keys does not follows
any orders etc.
list02 = ['pete', 'yuno', 'cleve', 'adam']


And so, I am trying to check the ordering between 2 lists, one from
Outliner, the other from a custom iterator, where list01 is the main list
to be check.
In this case, what I am trying to achieve is:

- if the order of list02 is different from list01, re-order the items in
list02 such that it follows the same hierarchical level in list01


About the part that I say I cannot use `list01 = list02` is because I will
be making use of the other values within that dicitonary (where it derive
the list02) in a different application, and sorry that I mentioned about
`sorted`, ignore it as I seems to have make the question more confusing.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d6c9066b-e3f2-4186-b544-98bea96b5726%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Kurian O.S
2018-10-10 21:44:24 UTC
Permalink
Sort can do that


In [2]: list01=['cleve', 'adam', 'yuno', 'pete']

In [3]: list02 = ['pete', 'yuno', 'cleve', 'adam']

In [4]: list02 = sorted(list2, key=list01.index)

In [5]: list02
Out[5]: ['cleve', 'adam', 'yuno', 'pete']
Post by kiteh
Hi Justin, sorry that I may have made my question confusing/ I was
babbling, typing away of the way I am trying to phrase my question.
Let me try again :)
1. Deriving the hierarchy from Outliner
Eg. This is the hierarchical level as seen in my Outliner
|-- base
|--|-- names
|--|--|-- cleve
|--|--|-- adam
|--|--|-- yuno
|--|--|-- pete
If I run a cmds command as follows, note that the result is as what I will
be seeing in the Outliner.
list01 = cmds.listRelatives('base', ad=True, f=True)[:-1]
print all_items
"""
['|base|name|cleve',
'|base|name|adam',
'|base|name|yuno',
'|base|name|pete']
"""
2. As mentioned, list02 is derived from dictionary keys from a custom
module, but the result/ order from the dictionary's keys does not follows
any orders etc.
list02 = ['pete', 'yuno', 'cleve', 'adam']
And so, I am trying to check the ordering between 2 lists, one from
Outliner, the other from a custom iterator, where list01 is the main list
to be check.
- if the order of list02 is different from list01, re-order the items
in list02 such that it follows the same hierarchical level in list01
About the part that I say I cannot use `list01 = list02` is because I will
be making use of the other values within that dicitonary (where it derive
the list02) in a different application, and sorry that I mentioned about
`sorted`, ignore it as I seems to have make the question more confusing.
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/d6c9066b-e3f2-4186-b544-98bea96b5726%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/d6c9066b-e3f2-4186-b544-98bea96b5726%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
--:: Kurian ::--
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CANEMyhM3UEYTevSG97UrekfCkAW26Yac10Ut3vhjkLmmaWh-JA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-10-11 00:36:56 UTC
Permalink
Post by Kurian O.S
Sort can do that
In [2]: list01=['cleve', 'adam', 'yuno', 'pete']
In [3]: list02 = ['pete', 'yuno', 'cleve', 'adam']
In [4]: list02 = sorted(list2, key=list01.index)
In [5]: list02
Out[5]: ['cleve', 'adam', 'yuno', 'pete']
Nice example.

I was thrown by the original question that had said the two lists contain
the same items. But as it turns out from the revised question, the two
lists contain different number of items and one needs to be sorted by the
other.

I noticed the example is using listRelatives() with all descendants. Not
sure what this looks like if there are children at different levels. Maybe
they are all normalized to just single words.
Post by Kurian O.S
Post by kiteh
Hi Justin, sorry that I may have made my question confusing/ I was
babbling, typing away of the way I am trying to phrase my question.
Let me try again :)
1. Deriving the hierarchy from Outliner
Eg. This is the hierarchical level as seen in my Outliner
|-- base
|--|-- names
|--|--|-- cleve
|--|--|-- adam
|--|--|-- yuno
|--|--|-- pete
If I run a cmds command as follows, note that the result is as what I
will be seeing in the Outliner.
list01 = cmds.listRelatives('base', ad=True, f=True)[:-1]
print all_items
"""
['|base|name|cleve',
'|base|name|adam',
'|base|name|yuno',
'|base|name|pete']
"""
2. As mentioned, list02 is derived from dictionary keys from a custom
module, but the result/ order from the dictionary's keys does not follows
any orders etc.
list02 = ['pete', 'yuno', 'cleve', 'adam']
And so, I am trying to check the ordering between 2 lists, one from
Outliner, the other from a custom iterator, where list01 is the main list
to be check.
- if the order of list02 is different from list01, re-order the items
in list02 such that it follows the same hierarchical level in list01
About the part that I say I cannot use `list01 = list02` is because I
will be making use of the other values within that dicitonary (where it
derive the list02) in a different application, and sorry that I mentioned
about `sorted`, ignore it as I seems to have make the question more
confusing.
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/d6c9066b-e3f2-4186-b544-98bea96b5726%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/d6c9066b-e3f2-4186-b544-98bea96b5726%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
--:: Kurian ::--
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CANEMyhM3UEYTevSG97UrekfCkAW26Yac10Ut3vhjkLmmaWh-JA%40mail.gmail.com
<https://groups.google.com/d/msgid/python_inside_maya/CANEMyhM3UEYTevSG97UrekfCkAW26Yac10Ut3vhjkLmmaWh-JA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2CLa7pJsq9EfPOj5WVMstUUMV5XgS2rPzUuZTXZj%3Dpxg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
davidlatwe
2018-10-11 04:33:06 UTC
Permalink
Hi Kiteh,

Why not use `OrderedDict` in the first place ?
Seems like `list01` and `list02` both from the same scene, if you are able
to use `OrderedDict` instead of `dict`, you can save the order.
Or, just iterating `list01` and use as key to access the `dict` value,
isn't that also guaranty the order of the *value* you retrieved, which I
assume is the final goal ?

kitehæ–Œ 2018幎10月11日星期四 UTC+8䞊午3時32分41秒寫道
Post by kiteh
Hi everyone,
I have 2 list items which contains the same amount of items, and I am
trying to check the order of one list to another.
Eg.
# This is derived from maya.cmds
list01 = ['cleve', 'adam', 'yuno', 'pete']
# This is derived from dictionary keys in a way it is capturing the same
info from the scene but derived very differently
list02 = ['pete', 'yuno', 'cleve', 'adam']
I am unable to use `list01 = list02` or something similar, because both
lists are called from different applications and hence this question of
mine.
And the reason I had wanted to the order to be the same is so that the
application that uses `list02` will populate in the same hierarchical
manner as in `list01` (outliner).
And neither would I want to/ can use `sorted` as this will mess up my
order. What is the best way to go about this?
P.S: Sorry in advance if this sounds like a very noob question...
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/59db1aa1-a46d-41b2-b61a-3683eafb123e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...