Is this a dream or a nightmare? (Was Re: XML)

Michal Wallace sabren at manifestation.com
Sun Oct 8 12:37:35 EDT 2000


On Sun, 8 Oct 2000, Alex Martelli wrote:

Hey Alex,

I gave up perl a long time ago, and I use python for EVERYTHING, but
I gotta step in here..

 
> Here's the Python equivalent:
> 
> import fileinput
> 
> themap = {}
> 
> for line in fileinput.input():
>     for word in line.split():
>         themap.setdefault(word,[]).append(fileinput.lineno())
> 
> words = themap.keys()
> words.sort()
> 
> for word in words:
>     print word+":",
>     for linum in themap[word]:
>         print linum,
>     print


Wait a second. setdefault is new in 1.6/2.0, neither of which
are the "real" python yet... So add two lines while themap.setdefault
becomes:

        if not themap.has_key(word):
            themap[word] = []
        themap[word].append(fileinput.lineno())


(You've also got string methods in there, but changing it to "import
fileinput, string" and "string.split" doesn't add any lines)


> for a total of 12.  Where's the 0.48-line equivalent perl program, by
> the way?


.. for a total of 14 ... 

I couldn't find a way to do it in less than one full line in perl, but
I'm not a perl hacker anymore, and even then, I don't think I ever
wrote a oneliner.. even so:

while(<>){for(split){$i{$_}.=++$L." "}}for(sort keys %i){print"$_:$i{$_}\n"}

anyway, that's half a line away from being 1/25th the size of the
python version.. :)

But so what? Who wants to write code like that? 

hey-wasn't-someone-just-asking-for-a-code-obsfucator-ly-yr's,

- Michal
------------------------------------------------------------------------
www.manifestation.com  www.sabren.com  www.linkwatcher.com  www.zike.net
------------------------------------------------------------------------





More information about the Python-list mailing list