title: Python set intersections, any way to return elements from the larger set?
body: <p>When Python takes the intersection of two sets, it always returns elements from the smaller one, which is reasonable in nearly all cases, but I'm trying to do the opposite.</p>
  
  <p>In the piece of code below, note that the intersection yields an integer, not a float.</p>
  
  <pre><code>[in]  &gt;&gt;&gt; x = {1.0,2.0,3.0}
  [in]  &gt;&gt;&gt; y = {1}
  [in]  &gt;&gt;&gt; x.intersection(y)
  [out] &gt;&gt;&gt; {1}
  [in]  &gt;&gt;&gt; y.intersection(x)
  [out] &gt;&gt;&gt; {1}
  </code></pre>
  
  <p>If I want to get a float back, I have to use some heavy copying.</p>
  
  <pre><code>[in]  &gt;&gt;&gt; x - y
  [out] &gt;&gt;&gt; {2.0,3.0}
  [in]  &gt;&gt;&gt; x - (x - y)
  [out] &gt;&gt;&gt; {1.0}
  </code></pre>
  
  <p>I'm dealing with much larger sets than the example above.  My question is whether there's any way to trick Python set.intersection method into returning elements from the larger set, or if there another method that can return the float 1.0 besides what I've done here.</p>
  
  <p>The reason why I'm doing this in the first place is I'm trying to implement a frozen dictionary in pure python by sub-classing frozenset.  I'm storing the key-value pairs using a subclass of tuple I call "Item" where <strong>hash</strong> returns the hash of the key only.  Using the code below, I'm able to create a set with a single key-value pair inside of it.  Then I extract the attribute "value" and return it.</p>
  
  <pre><code>def __getitem__(self, key):
      wrapped = Item((key,),flag=False)
      if not frozenset.__contains__(self, wrapped):
          raise KeyError(key)
      matches = self - (self - {wrapped})
      for pair in frozenset.__iter__(matches):
          return pair.value
  </code></pre>
  
  <p>I know that the copying is the reason for the slowness because when I try to return an item whose key is not in the dictionary, I get a KeyError immediately, even for sets with 10 million items.</p>