擅长:python、mysql、java
<p>一种方法是使用<code>list comprehension</code>来构造请求的列表。<br/>
本质上,我们在外循环中遍历<code>list</code>,在内循环中遍历<code>dictionary</code>,然后将<code>list</code>值与<code>dictionary</code>中的<code>key</code>值进行比较,如果有匹配项,则将关联的<code>key</code>值保存在新的输出列表中</p>
<p>下面的代码段如上所述工作:</p>
<pre><code>my_dict = {'ABC':'Apple','DEF':'Mango','GHI':'Pear','JKL':'Orange','MNO':'Plum'}
lst_x = ['DEF','GHI']
out = [value for element in lst_x for key, value in my_dict.items() if element == key]
print(out)
</code></pre>
<p>运行时,它会打印:</p>
<pre><code>['Mango', 'Pear']
</code></pre>