Set TypesΒΆ

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. There are two built-in types in Python: set and frozenset. However, both types are not supported in RPython.

Attention

sets are not directly supported in RPython. Instead you should use a plain dict and fill the values with None. Values in that dict will not consume space.

Right now, we can use dict to simulate a set.

def sets():
    # Since set is not supported in RPython, we use dict with None as values to
    # simulate a set.
    s1 = {'foo': None, 'bar': None, 'baz': None, 'foo': None, 'baz': None}
    if 'fooo' not in s1: print 'fooo is not in the set s1'
    if 'foo' in s1: print 'foo is in the set s1'
    for i in s1: print i

    s2 = {'foo': None, 'hello': None, 'world': None, 'foo': None}

    # union
    def set_union(s1, s2):
        s3 = s1.copy()
        s3.update(s2)
        return s3

    print 'union:'
    s3 = set_union(s1, s2)
    for i in s3: print i

    # intersection:
    def set_intersection(s1, s2):
        s3 = {}
        for i in s1:
            if i in s2:
                s3.update({i: None})

        for i in s2:
            if i in s1:
                s3.update({i: None})
        return s3

    print 'intersection:'
    s3 = set_intersection(s1, s2)
    for i in s3: print i

def entry_point(argv):
    sets()
    return 0

def target(*args): return entry_point
if __name__ == "__main__":
    import sys
    entry_point(sys.argv)

All set operations and methods in the following code are not supported in RPython.

################### UNSUPPORTED IN RPYTHON ######################

def sets_unsupported():
    s1 = set(['foo', 'bar', 'baz', 'foo'])
    s2 = set(['foo', 'hello', 'world', 'foo'])
    if 'fooo' not in s1: print 'fooo is not in the set s1'
    if 'foo' in s1: print 'foo is in the set s1'
    print s1

    # union
    print s1.union(s2)
    print s1 | s2

    # intersection
    print s1.intersection(s2)
    print s1 & s2

    # difference
    print s1.difference(s2)
    print s1 - s2

    # symmetric difference
    print s1.symmetric_difference(s2)
    print s1 ^ s2

    # whether or not two sets have any elements in common
    print s1.isdisjoint(s2)

    # whether one set is a subset/superset of the other
    print s1.issubset(s2)
    print s1 <= s2
    print s1.issubset(s2)
    print s1 >= s2

    # whether on set is a proper subset/superset of the other
    print s1 < s2
    print s1 > s2

    # modifying a set
    s1 |= s2
    s1.update(['fob', 'bah'])
    s1.add('boz')
    s1.remove('boz')
    s1.pop()
    s2.clear()
    print s1

    fs = frozenset(['foo', 'bar', 'baz'])    # frozenset is immutable
    print fs

def entry_point(argv):
    sets_unsupported()
    return 0

def target(*args): return entry_point
if __name__ == "__main__":
    import sys
    entry_point(sys.argv)