lavinog
July 27th, 2009, 09:58 PM
I am working on a python project that involves finding numbers in sets that don't exist in any other set.
The code I have looks like it works, but it seems like there should be an easier way to do this:
(make_unique is the function in question)
def make_unique(setdata):
'''Remove values in each set that exist in another set'''
keys=setdata.keys()
keys_checked=list() # list of keys that have been checked
notunique=set() # set of values that exist in more than one set
for key1 in keys:
keys_checked.append(key1)
for key2 in keys:
if key2 in keys_checked : continue # skip already iterated key
for n in setdata[key1] :
if n in notunique : continue # already know it isn't unique
if n in setdata[key2] :
notunique.add(n) # n not unique
continue
for key in keys:
# remove non-unique numbers
setdata[ key ] = setdata[ key ] - notunique
def makedata():
'''Create dictionary of sets for testing'''
import random
setdata=dict()
for key in 'abcdef':
count=random.randrange(1000,10000)
nlist=set()
for n in range(count):
nlist.add(random.randrange(10000) )
setdata[key]=nlist
return setdata
def printset(setdata):
for key in setdata.keys():
print( key,len( setdata[ key ] ) )
setdata=makedata()
printset(setdata)
make_unique(setdata)
print('-'*30)
printset(setdata)
The code I have looks like it works, but it seems like there should be an easier way to do this:
(make_unique is the function in question)
def make_unique(setdata):
'''Remove values in each set that exist in another set'''
keys=setdata.keys()
keys_checked=list() # list of keys that have been checked
notunique=set() # set of values that exist in more than one set
for key1 in keys:
keys_checked.append(key1)
for key2 in keys:
if key2 in keys_checked : continue # skip already iterated key
for n in setdata[key1] :
if n in notunique : continue # already know it isn't unique
if n in setdata[key2] :
notunique.add(n) # n not unique
continue
for key in keys:
# remove non-unique numbers
setdata[ key ] = setdata[ key ] - notunique
def makedata():
'''Create dictionary of sets for testing'''
import random
setdata=dict()
for key in 'abcdef':
count=random.randrange(1000,10000)
nlist=set()
for n in range(count):
nlist.add(random.randrange(10000) )
setdata[key]=nlist
return setdata
def printset(setdata):
for key in setdata.keys():
print( key,len( setdata[ key ] ) )
setdata=makedata()
printset(setdata)
make_unique(setdata)
print('-'*30)
printset(setdata)