擅长:python、mysql、java
<p>使用<code>defaultdict</code>的解决方案。将名字存储为dict的键,每个名字加1。你知道吗</p>
<pre><code>from collections import defaultdict
result = defaultdict(int)
my_list = ["David Smith", "John Wilson", "Mike Davis", "Shawn Jones",
"Shawn Gilmour", "David Berman"]
for first, last in map(lambda x: x.split(), my_list):
result[first] += 1
David: 2
John: 1
Mike: 1
Shawn: 2
</code></pre>
<p>如果需要的话,您可以使用<code>defaultdict(list)</code>并附加issue <code>result[first].append(last)</code>,它将把名字作为键,把姓氏列表作为它的值,如果出现用例,这些值可能会很有用。你知道吗</p>