擅长:python、mysql、java
<p>像这样的</p>
<pre class="lang-py prettyprint-override"><code>contacts = {}
with open('contacts.txt', 'r') as f:
name = None
for line in f:
parts = line.rstrip().split(': ')
if parts[0] == 'Name':
name = parts[1]
if name not in contacts:
contacts[name] = {} # Initialize empty dict for new name
elif parts[0] == 'Email':
email = parts[1]
if email not in contacts[name]:
contacts[name][email] = 0 # Initialize count for new email
contacts[name][email] += 1
print(contacts) # Outputs: {'John Doe': {'john.doe@exabeam.com': 3}, 'Martina jackson': {'Martina.jackson@exabeam.com': 1}, 'Steve Bob': {'steve.bob@exabeam.com': 2}}
</code></pre>