Member Avatar for gianniszaf

Hi there,
I am totally new in Python. What I am trying to do is the following:

I have in a file various email addresses. The file look like

manolis@gmail.com
giannis@gmail.com
kostas@yahoo.com
eirini@yahoo.com
ssss@aol.com
wertza@yahoo.gr
xristhanthi@gmail.com

and I want to count how many addresses belong to each provider.

Eg.
aol.com: 1
gmail.com :3
yahoo.com: 2
yahoo.gr: 1

I tried by using split, and word count but no luck.

Can somebody help me please?

The trick is to split your email data into a list of lines first, and the split each line into user, provider at the @ character. For example:

email = """\
manolis@gmail.com
giannis@gmail.com
kostas@yahoo.com
eirini@yahoo.com
ssss@aol.com
wertza@yahoo.gr
xristhanthi@gmail.com
"""

# split into list of lines
lines = email.split()

provider_freq = {}
for item in lines:
    # split each line item at the @ character
    user, provider = item.split('@')
    #print user, provider  # test
    # build the dictionary
    count = provider_freq.get(provider, 0)
    provider_freq[provider] = count + 1
    
print provider_freq

"""
my output -->
{'yahoo.com': 2, 'aol.com': 1, 'gmail.com': 3, 'yahoo.gr': 1}
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.