I am doing a dry-run test of my code and bump into an issue...
Please see my screenshot for my very simple hierarchy as I am trying to do renaming.

This is my code:
import maya.cmds as cmds
all_grp = []
for child_grp in cmds.listRelatives("group", path=True):
print child_grp
all_grp.append(child_grp)
all_loc = cmds.listRelatives(cmds.ls(type='locator'), parent = True)
for grp, loc in zip(all_grp, all_loc):
print "grp - {0}, loc - {1}".format(grp, loc)
What I am expecting to see:
# Correct
grp - sub_group1, loc - locator1
grp - sub_group3, loc - locator3
grp - sub_group2, loc - locator2
But my dry-run result says otherwise:
# Wrong
grp - sub_group1, loc - locator1
grp - sub_group3, loc - locator2
grp - sub_group2, loc - locator3
Notice that in both sub_group3 and sub_group2, I am expecting the loc to be grabbing locator3 instead of locator2 (for sub_group3)
How do I iterate such that 'loc' will checks if it belongs within the hierarchy of 'grp'?
I am trying to achieve something like this:
If loc belongs in this grp, then do this processA...
if loc does not belong in that grp, loop thru the grp list to find its 'parent/grp' then do processA..