473,472 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to sort very large arrays?

kj

I'm downloading some very large tables from a remote site. I want
to sort these tables in a particular way before saving them to
disk. In the past I found that the most efficient way to do this
was to piggy-back on Unix's highly optimized sort command. So,
from within a Perl script, I'd create a pipe handle through sort
and then just print the data through that handle:

open my $out, "|$sort -t '\t' -k1,1 -k2,2 -u $out_file" or die $!;
print $out $_ for @data;

But that's distinctly Perlish, and I'm wondering what's the "Python
Way" to do this.

TIA!

kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #1
4 1765
On Fri, 13 Jun 2008 17:54:32 +0000, kj wrote:
I'm downloading some very large tables from a remote site. I want to
sort these tables in a particular way before saving them to disk. In
the past I found that the most efficient way to do this was to
piggy-back on Unix's highly optimized sort command. So, from within a
Perl script, I'd create a pipe handle through sort and then just print
the data through that handle:

open my $out, "|$sort -t '\t' -k1,1 -k2,2 -u $out_file" or die $!;
print $out $_ for @data;

But that's distinctly Perlish, and I'm wondering what's the "Python Way"
to do this.

TIA!

kynn
os.system and os.popen are much like what you'd find in C.

The subprocess module is more specific to python, and is a little more
complicated but more powerful.

Jun 27 '08 #2

"kj" <so***@987jk.com.invalidwrote in message
news:g2**********@reader2.panix.com...
| I'm downloading some very large tables from a remote site. I want
| to sort these tables in a particular way before saving them to
| disk. In the past I found that the most efficient way to do this
| was to piggy-back on Unix's highly optimized sort command. So,

If the tables can fit in memory as a list of key,text tuples and if they
have some of the non-random structure exploited by Python's current
list.sort (only documented, as far as I know, either in the source or test
code, not sure), then you might consider that. Otherwise, use the system
sort.

Jun 27 '08 #3
On Jun 14, 1:54 am, kj <so...@987jk.com.invalidwrote:
I'm downloading some very large tables from a remote site. I want
to sort these tables in a particular way before saving them to
disk. In the past I found that the most efficient way to do this
was to piggy-back on Unix's highly optimized sort command. So,
from within a Perl script, I'd create a pipe handle through sort
and then just print the data through that handle:
This is a python clone of your code from a python rookie :)

from os import popen

p = popen("sort -t '\t' -k1,1 -k2,2 -u %s" % out_file)
for line in data:
print >p, line

there is no "die $!" here, I think it is good to let python
throw the exception to your console
>
open my $out, "|$sort -t '\t' -k1,1 -k2,2 -u $out_file" or die $!;
print $out $_ for @data;

But that's distinctly Perlish, and I'm wondering what's the "Python
Way" to do this.

TIA!

kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #4
On Jun 14, 1:54 am, kj <so...@987jk.com.invalidwrote:
I'm downloading some very large tables from a remote site. I want
to sort these tables in a particular way before saving them to
disk. In the past I found that the most efficient way to do this
was to piggy-back on Unix's highly optimized sort command. So,
from within a Perl script, I'd create a pipe handle through sort
and then just print the data through that handle:
This is a python clone of your code from a python rookie :)

from os import popen

p = popen("sort -t '\t' -k1,1 -k2,2 -u %s" % out_file)
for line in data:
print >p, line

there is no "die $!" here, I think it is good to let python
throw the exception to your console
>
open my $out, "|$sort -t '\t' -k1,1 -k2,2 -u $out_file" or die $!;
print $out $_ for @data;

But that's distinctly Perlish, and I'm wondering what's the "Python
Way" to do this.

TIA!

kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #5

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

Similar topics

11
by: JerryJ | last post by:
I have to fetch values from a database and store them in a vector in my c++ application. What is faster? if i fetch the values sorted from the database with a 'order by' clause in the...
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
9
by: David | last post by:
Hi all, I have an array of strings. For example... = "entry1 first entry"; = "entry2 second entry"; = "entry3 third entry"; etc... How can I sort this array by the number after "entry"?
19
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray =...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
2
by: Tobias Froehlich | last post by:
How can I sort an array so that the highest value has the index of 0 ? I mean if i have a double array like this: { 0.1, 0.5, 0.32, 0.9 } it should be transformed into a new array that looks...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
13
by: ralphedge | last post by:
These sorts work fine on 100000 ints but if I go much higher they will both segmentation fault **************************MERGESORT********************* mergesort(int *a, int size) //a is...
10
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.