擅长:python、mysql、java
<p>通过使用<code>items()</code>更简洁的方法:</p>
<pre><code>animals = {'tiger': ['claws', 'sharp teeth', 'four legs', 'stripes'],
'elephant': ['trunk', 'four legs', 'big ears', 'gray skin'],
'human': ['two legs', 'funny looking ears', 'a sense of humor']
}
# some dummy function
def bandit_language(val):
return 'Ho ho ho'
def myfunction(animals):
return {key: [bandit_language(val) for val in lst] for key, lst in animals.items()}
print(myfunction(animals)
</code></pre>
<p>这将产生:</p>
<pre><code>{'human': ['Ho ho ho', 'Ho ho ho', 'Ho ho ho'], 'tiger': ['Ho ho ho', 'Ho ho ho', 'Ho ho ho', 'Ho ho ho'], 'elephant': ['Ho ho ho', 'Ho ho ho', 'Ho ho ho', 'Ho ho ho']}
</code></pre>