Search code examples
pythonlistsortingfiltermaya

How i can sort a list python maya


I have a list [0, 0, 0, 0.5, 0.5] and I would like to sort it to keep only values ​​that are not equal to 0.

from maya import cmds

def splitShape():
    a = cmds.ls(sl=True)

    if len(a)<=1:
        cmds.warning( "Select 2 target")

    elif len(a) >= 2:
        #cmds.warning( "SPLIIIIIT")
        cmds.select( a[0] , r = True)
        Vertex = a[0] + '.vtx[*]'
        cmds.select(Vertex)

        #_____LIST______
        b = cmds.getAttr(a[0] + 'Shape' + '.pnts[*]' + '.pntz')
        #print b

        if len(b) == 0:
            print ok
        elif len(b) != 0:
            cmds.setAttr( a[0] + 'Shape' + '.pnts[*]' + '.pntz' , 0)

Tanks :)


Solution

  • To filter a list keeping only values not equal to zero:

    a = [0, 0, 0, 0.5 ,0.5]
    list(filter(lambda x: x != 0, a))
    # [0.5, 0.5]