Lexicographical sort for numarray

Robert Kern rkern at ucsd.edu
Wed Apr 27 17:35:50 EDT 2005


Jason Mobarak wrote:
> It does what the OPs example does, but with numeric types.

It certainly does not.

In [15]:arr = na.arange(100)

In [16]:random.shuffle(arr)

In [17]:arr.shape = (10,10)

In [18]:arr2 = na.array(arr)

In [19]:L = arr.tolist()

In [20]:L.sort()

In [21]:na.array(L)
Out[21]:
array([[ 8, 22, 40, 85, 64, 20, 91, 76, 19, 56],
        [11, 83, 86, 51, 72, 17, 80, 35,  1, 18],
        [13, 62,  0, 95, 25, 28,  5, 78, 54, 55],
        [41, 44, 15, 24, 27, 97, 31, 75, 65, 29],
        [45, 57, 16, 89, 87, 90, 42, 50, 93,  9],
        [53, 21, 47, 69,  2, 12, 92, 98, 66, 48],
        [70, 79, 36, 88, 10, 39, 81, 61, 43,  3],
        [74, 37, 49, 14, 33, 34, 52, 23, 96, 71],
        [82, 67, 30, 60,  6, 73, 99, 94,  7, 58],
        [84, 63,  4, 46, 26, 32, 59, 38, 68, 77]])

In [22]:arr2.flat.sort()

In [23]:arr2
Out[23]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
        [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
        [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
        [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
        [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
        [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list