How to JSON serialize sets?

You can create a custom encoder that returns a list when it encounters a set. Here’s an example: >>> import json >>> class SetEncoder(json.JSONEncoder): … def default(self, obj): … if isinstance(obj, set): … return list(obj) … return json.JSONEncoder.default(self, obj) … >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) ‘[1, 2, 3, 4, 5]’ You can detect other types this way … Read more

Set value of hidden field in a form using jQuery’s “.val()” doesn’t work

:text will fail for a input with a type value of hidden. It’s also much more efficient to just use: $(“#texens”).val(“tinkumaster”); ID attributes should be unique on a web page, make sure yours are as this may contribute to any problems you’re having, and specifying a more complicated selector just slows things down and doesn’t … Read more

Java Set retain order?

The Set interface does not provide any ordering guarantees. Its sub-interface SortedSet represents a set that is sorted according to some criterion. In Java 6, there are two standard containers that implement SortedSet. They are TreeSet and ConcurrentSkipListSet. In addition to the SortedSet interface, there is also the LinkedHashSet class. It remembers the order in … Read more

How is set() implemented?

According to this thread: Indeed, CPython’s sets are implemented as something like dictionaries with dummy values (the keys being the members of the set), with some optimization(s) that exploit this lack of values So basically a set uses a hashtable as its underlying data structure. This explains the O(1) membership checking, since looking up an … Read more

Getting the difference between two sets

Try this test2.removeAll(test1); Set#removeAll Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.