473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Lexicographical sort for numarray

Suppose arr is a two dimensional numarray array. Suppose we do the
following:

def rowsort(arr):
a = arr.tolist()
a.sort()
return numarray.array(a)

Can this sort be done efficiently in numarray? This function is called
"rowsort" in MatLab.
Jul 19 '05 #1
5 2137
import numarray as na
import random

# example array
arr = range(100)
random.shuffle(arr)
arr = na.array(arr)
arr = na.reshape(arr, (10,10))

print arr # not rowsort'ed

arr.flat.sort() # key line

print arr # rowsort'ed

Jul 19 '05 #2
Jason Mobarak wrote:
import numarray as na
import random

# example array
arr = range(100)
random.shuffle(arr)
arr = na.array(arr)
arr = na.reshape(arr, (10,10))

print arr # not rowsort'ed

arr.flat.sort() # key line

print arr # rowsort'ed


That's definitely not a lexicographic sort.

--
Robert Kern
rk***@ucsd.edu

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

Jul 19 '05 #3
It does what the OPs example does, but with numeric types.

Jul 19 '05 #4
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
rk***@ucsd.edu

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

Jul 19 '05 #5
Ah, okay, I didn't remember correctly what arr.tolist did. My mistake.

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
3
by: Alexander Schwaigkofler | last post by:
Hi! I have the following problem with numarray. I read the install.txt manual, but it doesn't already work. OS: Microsoft Windows 2000 python: Python 2.2.3 (#42, May 30 2003, 18:12:08) on...
0
by: andrewfelch | last post by:
Below is the code to/from Boolean arrays and Unsigned integers. On my Pentium 4, functions such as "bitwise_and" are 32 times faster when run on 32-bit integers instead of the...
1
by: tim.lino | last post by:
Dear all, I would like to sort the elements in a vector in a lexicographical order. The elements are defined as: class E { public: int x; int y;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.