擅长:python、mysql、java
<pre><code>def preimage(b: B, f: Dict[A, B]) -> Set[A]:
''' get the preimage of one item. '''
return set(a for a in f.keys() if f[a]==b)
</code></pre>
<p>假设<a href="https://www.python.org/dev/peps/pep-0484/" rel="nofollow noreferrer">TypeVar and other things from pep484</a></p>
<pre><code>from typing import TypeVar, Dict, Set
A = TypeVar('A')
B = TypeVar('B')
</code></pre>
<p>如果没有类型注释</p>
<pre><code>def preimage(b, f):
...
</code></pre>
<p>或者是密码域的一个子集</p>
<pre><code>from typing import Collection
from functools import reduce
def preimage_(bs: Collection[B], f: Dict[A, B]) -> Set[A]:
''' get the preimage of a collection of items '''
return reduce(lambda a, b: a.union(b), [preimage(b, f) for b in bs])
</code></pre>