Counting distinct sets in a multiset in Python

If like me you suck at Python you may want to do convoluted things like count the distinct sets in some multiset. Here's one way you can do it.

In this example sets are read into a defaultdict and in this case set 1 is identical to set 3. So out of the 4 sets read into the dictionary there are 3 distinct sets.

import collections

def getDistinctSets(dupedList):
	new_set = set()
	for k,v in m.iteritems():
		new_set.add(frozenset(v))
	return new_set

m = collections.defaultdict(set)
m[0].add("ham")
m[1].add("ham")
m[1].add("cheese")
m[2].add("ham")
m[2].add("cheese")
m[2].add("pineapple")
m[3].add("cheese")
m[3].add("ham")

print getDistinctSets(m)

This returns a set of all the distinct sets, minus the duplicates:

set([
	frozenset(['cheese', 'ham']),
	frozenset(['ham']),
	frozenset(['cheese', 'ham', 'pineapple'])
])
This entry was posted in Programming, Python. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>