Sorting coordinates array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten van Reeuwijk

    Sorting coordinates array

    I'm a newbie to Python, so sorry for this maybe trivial question. I have a
    numpy array with coordinates, which I want to sort, for example first on
    z-coordinate, then x and lastly y-coordinate. So an array like:

    [[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]]

    should look after sorting like

    [[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]]

    I tried the sort command, but that command mangles my coordinate pairs. Also
    converting the array to a list doesn't seem to improve the results. How
    should I tackle this problem?

    TIA, Maarten

    --
    =============== =============== =============== =============== =======
    Maarten van Reeuwijk Heat and Fluid Sciences
    Phd student dept. of Multiscale Physics
    www.ws.tn.tudelft.nl Delft University of Technology
  • Ville Vainio

    #2
    Re: Sorting coordinates array

    Maarten van Reeuwijk <maarten@remove _this_ws.tn.tud elft.nl> writes:
    [color=blue]
    > I'm a newbie to Python, so sorry for this maybe trivial question. I have a
    > numpy array with coordinates, which I want to sort, for example first on
    > z-coordinate, then x and lastly y-coordinate. So an array like:[/color]

    Implement a comparison function:
    [color=blue][color=green][color=darkred]
    >>> help([].sort)[/color][/color][/color]
    Help on built-in function sort:

    sort(...)
    L.sort(cmpfunc= None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1


    Alternatively, you could take a look at DSU pattern (Decorate, Sort,
    Undecorate):



    --
    Ville Vainio http://www.students.tut.fi/~vainio24

    Comment

    • Peter Otten

      #3
      Re: Sorting coordinates array

      Maarten van Reeuwijk wrote:
      [color=blue]
      > I'm a newbie to Python, so sorry for this maybe trivial question. I have a
      > numpy array with coordinates, which I want to sort, for example first on
      > z-coordinate, then x and lastly y-coordinate. So an array like:
      >
      > [[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]]
      >
      > should look after sorting like
      >
      > [[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]]
      >
      > I tried the sort command, but that command mangles my coordinate pairs.
      > Also converting the array to a list doesn't seem to improve the results.
      > How should I tackle this problem?[/color]
      [color=blue][color=green][color=darkred]
      >>> import Numeric
      >>> a = Numeric.array([[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]])
      >>> lst = a.tolist()
      >>> lst.sort(lambda x, y: cmp(x[::-1], y[::-1]))
      >>> lst[/color][/color][/color]
      [[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      You can pass an arbitrary comparison function to list.sort(), I just compare
      reversed copies of the inner lists.
      This is a highly unoptimized version, but at least seems to do the job.

      Peter

      Comment

      • Peter Otten

        #4
        Re: Sorting coordinates array

        Peter Otten wrote:
        [color=blue]
        > This is a highly unoptimized version, but at least seems to do the job.[/color]

        Slightly better:
        [color=blue][color=green][color=darkred]
        >>> lst = a.tolist()
        >>> lst[/color][/color][/color]
        [[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]][color=blue][color=green][color=darkred]
        >>> for item in lst: item.reverse()[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> lst.sort()
        >>> for item in lst: item.reverse()[/color][/color][/color]
        ....[color=blue][color=green][color=darkred]
        >>> lst[/color][/color][/color]
        [[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]][color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Peter

        Comment

        Working...