Re: Perl and Python, a practical side-by-side example.

2007-03-04 Thread Bruno Desthuilliers
Peter Otten a écrit : > Bruno Desthuilliers wrote: > > >> print >> output, sorted(decorated_lines, reverse=True)[0][1] > > > Or just >print >> output, max(decorated_lines)[1] Good point. More explicit, and a bit faster too. Thanks Peter. -- http://mail.python.org/mailman/l

Re: Perl and Python, a practical side-by-side example.

2007-03-04 Thread Peter Otten
Bruno Desthuilliers wrote: > print >> output, sorted(decorated_lines, reverse=True)[0][1] Or just print >> output, max(decorated_lines)[1] Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: Perl and Python, a practical side-by-side example.

2007-03-04 Thread Bruno Desthuilliers
Shawn Milo a écrit : (snip) > The script reads a file from standard input and > finds the best record for each unique ID (piid). The best is defined > as follows: The newest expiration date (field 5) for the record with > the state (field 1) which matches the desired state (field 6). If > there is

Re: Perl and Python, a practical side-by-side example.

2007-03-04 Thread Bruno Desthuilliers
Bjoern Schliessmann a écrit : > Bruno Desthuilliers wrote: > >>Shawn Milo a écrit : > > >>>if recs.has_key(piid) is False: >> >>'is' is the identity operator - practically, in CPython, it >>compares memory addresses. You *dont* want to use it here. > > > It's recommended to use "is None";

Re: Perl and Python, a practical side-by-side example.

2007-03-04 Thread Bruno Desthuilliers
John Machin a écrit : > On Mar 3, 12:36 pm, Bruno Desthuilliers > > [snip] > >> DATE = 5 >> TARGET = 6 > > [snip] > >>Now for the bad news: I'm afraid your algorithm is broken : here are my >>test data and results: >> >>input = [ >> #ID STATE ... ... ... TARG DATE >> "aaa\tAAA

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread Jussi Salmela
Shawn Milo kirjoitti: > > I am not looking for the smallest number of lines, or anything else > that would make the code more difficult to read in six months. Just > any instances where I'm doing something inefficiently or in a "bad" > way. > > I'm attaching both the Perl and Python versions, an

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread Paddy
On Mar 2, 10:44 pm, "Shawn Milo" <[EMAIL PROTECTED]> wrote: > I'm new to Python and fairly experienced in Perl, although that > experience is limited to the things I use daily. > > I wrote the same script in both Perl and Python, and the output is > identical. The run speed is similar (very fast) a

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread Ben Finney
William Heymann <[EMAIL PROTECTED]> writes: > On Saturday 03 March 2007, Ben Finney wrote: > > Bjoern Schliessmann <[EMAIL PROTECTED]> writes: > > > > if not recs.has_key(piid): # [1] > > > Why not > > if piid not in recs: > > That is shorter, simpler, easier to read and very slightly fas

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread William Heymann
On Saturday 03 March 2007, Ben Finney wrote: > Bjoern Schliessmann <[EMAIL PROTECTED]> writes: > > if not recs.has_key(piid): # [1] > Why not if piid not in recs: That is shorter, simpler, easier to read and very slightly faster. Plus you can change the data structure of recs later with

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread John Machin
On Mar 3, 7:08 pm, [EMAIL PROTECTED] wrote: > On Mar 2, 2:44 pm, "Shawn Milo" <[EMAIL PROTECTED]> wrote: > > (snipped) > > > I'm attaching both the Perl and Python versions, and I'm open to > > comments on either. The script reads a file from standard input and > > finds the best record for each un

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Bjoern Schliessmann wrote: > Bruno Desthuilliers wrote: >> Shawn Milo a écrit : > >>> if recs.has_key(piid) is False: >> >> 'is' is the identity operator - practically, in CPython, it >> compares memory addresses. You *dont* want to use it here. > > It's recommended

Re: Perl and Python, a practical side-by-side example.

2007-03-03 Thread attn . steven . kuo
On Mar 2, 2:44 pm, "Shawn Milo" <[EMAIL PROTECTED]> wrote: (snipped) > I'm attaching both the Perl and Python versions, and I'm open to > comments on either. The script reads a file from standard input and > finds the best record for each unique ID (piid). The best is defined > as follows: The ne

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread Ben Finney
Bjoern Schliessmann <[EMAIL PROTECTED]> writes: > Bruno Desthuilliers wrote: > > Shawn Milo a écrit : > > >> if recs.has_key(piid) is False: > > > > 'is' is the identity operator - practically, in CPython, it > > compares memory addresses. You *dont* want to use it here. > > It's recommended

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread Bjoern Schliessmann
Bruno Desthuilliers wrote: > Shawn Milo a écrit : >> if recs.has_key(piid) is False: > > 'is' is the identity operator - practically, in CPython, it > compares memory addresses. You *dont* want to use it here. It's recommended to use "is None"; why not "is False"? Are there multiple False in

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread John Machin
On Mar 3, 12:36 pm, Bruno Desthuilliers > [snip] > DATE = 5 > TARGET = 6 [snip] > Now for the bad news: I'm afraid your algorithm is broken : here are my > test data and results: > > input = [ > #ID STATE ... ... ... TARG DATE > "aaa\tAAA\t...\t...\t...\tBBB\t20071212\n", [sn

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread Paul Rubin
Here's my version (not tested much). Main differences from yours: 1. It defines a Python class to hold row data, and defines the __cmp__ operation on the class, so given two Row objects r1,r2, you can say simply if r1 > r2: ... to see which is "better". 2. Instead of reading all the rows in

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread Bruno Desthuilliers
John Machin a écrit : > On Mar 3, 9:44 am, "Shawn Milo" <[EMAIL PROTECTED]> wrote: > (snip) > > [big snip] > Here is my rewrite in what I regard as idiomatic reasonably-modern > Python (OMMV of course). (snip) John, I *swear* I didn't read your code before posting my own version ! -- http://m

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread Bruno Desthuilliers
Shawn Milo a écrit : > I'm new to Python and fairly experienced in Perl, although that > experience is limited to the things I use daily. > > I wrote the same script in both Perl and Python, and the output is > identical. The run speed is similar (very fast) and the line count is > similar. > > N

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread John Machin
On Mar 3, 9:44 am, "Shawn Milo" <[EMAIL PROTECTED]> wrote: > I'm new to Python and fairly experienced in Perl, although that > experience is limited to the things I use daily. > > I wrote the same script in both Perl and Python, and the output is > identical. The run speed is similar (very fast) an

Re: Perl and Python, a practical side-by-side example.

2007-03-02 Thread bearophileHUGS
Few suggestions, some important, some less important. All my suggestions are untested. Use 4 spaces to indent. If you want to speed up this code you can move it inside a function. After that, if you want to make it even faster you can use Psyco too. Ho are the dates represented? How do you te

Perl and Python, a practical side-by-side example.

2007-03-02 Thread Shawn Milo
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The run speed is similar (very fast) and the line count is similar. Now that they're both working, I was l