[Tutor] Re: Ugly python one-liner !

Prahlad Vaidyanathan slime@vsnl.net
Fri, 12 Jul 2002 10:57:47 +0530


--6TrnltStXW4iwmi0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi,

On Tue, 09 Jul 2002 Derrick 'dman' Hudson spewed into the ether:
> import operator
>=20
> |     def one_norm (self) :
> |         """The sum of the elements in different rows of each column
> |         is computed, and the maximum of these values is said to
> |         be the one-norm of the Matrix.
> |         """
>           return max([reduce(operator.add,
> |                     [abs(self[r,c]) for r in range(self.rows)])
> |                         for c in range(self.columns)])
>=20
>=20
> |     Between map(), reduce() and list comprehension, I have finally
> | succeeded in making python look scary :-)
>=20
> Yep.  But you don't need that lambda in there -- the operator module

    Yes, I noticed the operator module, but I thought lambda would
involve much less over-head. I've actually only recently discovered
lambda's capabilities, when I tried this :

"""
def factorial (n) :
    return reduce(lambda x,y: x*y, range(2,n+1))
"""
> already has an add function for just that purpose :-).  You also don't
> need the backslashes -- python knows to continue because you haven't
> close the parenthesis.

    Ahh ! Didn't know that - python gets cooler by the minute :-)

pv.
--=20
Prahlad Vaidyanathan  <http://www.symonds.net/~prahladv/>

One good reason why computers can do more work than people is that they
never have to stop and answer the phone.

--6TrnltStXW4iwmi0
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9LmjTIKhjOSElu4YRAo4cAJ4uv1dvLPOZZLqXkTMJZ3E86UhYnQCgwyb6
ret8LaO7/HPdRJW9dtjTJEU=
=ynf7
-----END PGP SIGNATURE-----

--6TrnltStXW4iwmi0--