<p>我正在研究一个反向索引的搜索程序。索引本身是一个字典,其键是术语,其值本身是短文档的字典,ID号是键,其文本内容是值。</p>
<p>因此,要对两个词执行“和”搜索,我需要将它们的帖子列表(词典)相交。在Python中,有什么方法可以做到这一点(不一定过于聪明)?我一开始就用<code>iter</code>做了很长的尝试:</p>
<pre><code>p1 = index[term1]
p2 = index[term2]
i1 = iter(p1)
i2 = iter(p2)
while ... # not sure of the 'iter != end 'syntax in this case
...
</code></pre>
<pre><code>In [1]: d1 = {'a':1, 'b':4, 'f':3}
In [2]: d2 = {'a':1, 'b':4, 'd':2}
In [3]: d = {x:d1[x] for x in d1 if x in d2}
In [4]: d
Out[4]: {'a': 1, 'b': 4}
</code></pre>