D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] PHP makes things easy



On Thu, Sep 11, 2003 at 11:22:57PM +0100, Neil Williams wrote:
> $numerical = array_flip($alphabet);
> ksort($numerical);
> 
> Can Perl/Python/Java/etc. do the same job in less code???
> :-)

hendry@xxxxxxxxxx:~$ python2
Python 2.2.2 (#1, Nov 22 2002, 17:25:34)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> alphabet = ( "562389856", "123521623" , "898234234", "002342444" )
>>> stuff = (zip(alphabet, range(len(alphabet))))
>>> stuff
[('562389856', 0), ('123521623', 1), ('898234234', 2), ('002342444', 3)]
>>> stuff.sort()
>>> stuff
[('002342444', 3), ('123521623', 1), ('562389856', 0), ('898234234', 2)]


Two lines too I guess. Providing I understood the problem.

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly