Demo entry 3375779

zips

   

Submitted by anonymous on Dec 19, 2015 at 20:15
Language: Python. Code size: 793 Bytes.

from collections import defaultdict

def main():
    zips_and_users = defaultdict(int) # A dictionary that returns 0 as default if zipcode isn't present
    with open('zips.txt') as f: # Open file, obtain handler
        for line in f: # Iterate through each line
            user, zcode, _ = [str.strip(entry) for entry in line.split(',')] # Split line by `,` and make sure to remove any trailing blank spaces
            zips_and_users[zcode] = zips_and_users[zcode] + 1 # Key in with a zipcode, increasing the number of users by one

    # Print the final state of the dictionary
    for zcode, user_number in zips_and_users.iteritems():
        print("Zip Code {zcode}: {user_number} users".format(zcode=zcode, user_number=user_number))

if __name__ == "__main__":
    main()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).