Not Equal to Each Other?

Alex Martelli aleax at mail.comcast.net
Thu Nov 3 21:42:34 EST 2005


<ale.of.ginger at gmail.com> wrote:
   ...
> Rather than writing
> 
> cellboard[0] is not* (cellboard[1] and cellboard[2] and cellboard[3]
> and cellboard[4] ... cellboard[8])
> cellboard[1] is not (cellboard[0] and cellboard[2] and cellboard[3] and
> cellboard[4] ... cellboard[8])

Urgh... the fastest way to check that a list of N numbers has no
duplicates is:
    if len(set(thelist)) == len(thelist):
        print 'no duplicates'

But if your purpose is to generate N random samples out of a population
of M, look at function random.sample (in module random in the Pythons
standard library) and you'll do even better!-)


Alex



More information about the Python-list mailing list