[Tutor] Testing if a number occurs more than once

Yann Le Du yann.ledu@noos.fr
Mon Dec 2 14:16:01 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 2 Dec 2002, Michael Williams wrote:

> as not only convoluted, but slow (this is the dominant loop of the 
> program). There must be a better way! Any suggestions?

try :

def morethanone(l):
        for n in l:
                if l.count(n)>1:
                        return 1
        return 0


then this gives :

In [372]: l=[1,2,3,4,5]

In [373]: morethanone(l)
Out[373]: 0

In [374]: l=[1,2,3,4,5,23,2,48,23,90]

In [375]: morethanone(l)
Out[375]: 1


Yann
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: For info see http://quantumlab.net/pine_privacy_guard/

iD8DBQE966+Bt4mG++uo5yQRAlIpAKC+VwLgfyL57UfYfMxiZuhtKzWU6QCfXlbH
gdPFJF6z0oC3J0cjlaoFWoU=
=ceQX
-----END PGP SIGNATURE-----