testing for uniquness in a large list

Lol McBride newspost at lolmc.com
Wed Oct 20 06:01:18 EDT 2004


Hi All,
I'm looking for some help in developing a way to test for the uniqueness
of an item in a large list.To illustrate what I mean the code below is an
indicator of what I want to do but I'm hoping for a faster way than the
one shown.Basically,I have a list of 20 integers and I want to create
another list of 200000 unique subsets of 12 integers from this list.WhatI
have done here is to use the sample()function from the random module
and then compare the result to every item in the ints list to check for
uniqueness - as you can guess this takes an interminable amount of time to
grind through.Can someone please enlighten me as to how I can do this and
keep the amount of time to do it to a minimum?

Thanks for taking the time to read this and doubly so if you're able to
help.
Cheers,
Lol McBride

#!/usr/bin/python
from random import *
#seq is the pool to choose from
seq = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
# this is how many times to choose a unique selection
rlen = 200000
counter = 100
ints = [0]
while 1:
    # if this is the last value for rlen then we stop
    if rlen == 0:
	break
    intLen = len(ints)
    cnt = 0
    testVal = sample(seq,12)
    testVal.sort()
    if len(ints)>=counter:
	print len(ints)
	counter = counter+100
    while 1:
	if ints.count(testVal)==1:
            cnt = cnt+1
	    break
	intLen = intLen -1
	if intLen == 0:
	    break
	if cnt == 1:
		continue
	else:
	    ints.append(testVal)
	rlen = rlen -1




More information about the Python-list mailing list