Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://docs.python.org/2.7/library/index.html" rel="nofollow">Python Standard Library</a> is your friend:</p> <pre><code>from collections import defaultdict from itertools import combinations tagged = { 'apple': ['fruit', 'green', 'nature'], 'sometree': ['tree', 'green', 'wood', 'nature'], 'banana': ['fruit', 'nature'], 'someplant': ['green', 'wood', 'nature'], 'otherplant': ['green', 'wood', 'nature'] } tag_sets = defaultdict(set) for each, tags in tagged.items(): tag_sets[frozenset(tags)].add(each) for a, b in combinations(tag_sets, 2): if a &lt; b: tag_sets[a].update(tag_sets[b]) elif b &lt; a: tag_sets[b].update(tag_sets[a]) for k, v in tag_sets.items(): print '(%s): %s' % (', '.join(k), ', '.join(v)) </code></pre> <p>Result:</p> <pre><code>(wood, green, nature): sometree, someplant, otherplant (fruit, green, nature): apple (fruit, nature): banana, apple (green, wood, tree, nature): sometree </code></pre> <p>How it works: First, we transpose the mapping <em>item</em> -> <em>tag set</em> to <em>tag set</em> -> <em>set of items associated with said tag set</em>. Then we iterate over all distinct 2-sets of tag sets, represented by pairs (a, b) and check if either a or b is a proper subset of the other. If it is, we join the mapped items of the superset with the mapped items of the proper subset.</p> <p>[EDIT]</p> <p>Here is another solution:</p> <pre><code>from collections import defaultdict tagged = { 'apple': ['fruit', 'green', 'nature'], 'sometree': ['tree', 'green', 'wood', 'nature'], 'banana': ['fruit', 'nature'], 'someplant': ['green', 'wood', 'nature'], 'otherplant': ['green', 'wood', 'nature'] } intersection = set(tagged).intersection tag_sets = set() items = defaultdict(set) for item, tags in tagged.items(): tag_sets.add(frozenset(tags)) for tag in tags: items[tag].add(item) for tag_set in tag_sets: result = intersection(*(items[tag] for tag in tag_set)) print '(%s): %s' % (', '.join(tag_set), ', '.join(result)) </code></pre> <p>I don't know if it is any faster, since i don't have enough data to do a meaningful comparison.</p> <p>[/EDIT]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload