RelientFX
RelientFX

Reputation: 53

Printing elements related to information in 'dict'

Having a little trouble understanding "dict[]" command, if anyone could help me with this, it would be much appreciated.

This is the code I have so far: (I've put in where and what I need help with) import csv

inputYear = input('Please enter the year: ')
inFile = open('babyQldAll.csv', 'rU')
cvsFile = csv.reader(inFile, delimiter=',')

dict = {}

for row in  cvsFile:
    year, name, count, gender = row

if (year == inputYear) and (gender == 'Boy'):
    dict[name] =  count

print('Popular boy names in year %s are:' % inputYear)


# According to information in 'dict',  print (name, count) sorted by 'name' in alphabetical order         



print("Print boy names... ")    


inFile.close()

using the code above I'm trying to get an outcome of:

Popular boy names in year "2010" are: Aidan 119, Alex 224, Derek 143, Steven 212, Tom 111.....

from the following input:

File 2010:
2010, Steven, 212, Boy
2010, Mary, 423, Girl
2010 , Tom, 111, Boy
2010, Jessica, 223, Girl
(and so on)

Upvotes: 0

Views: 96

Answers (2)

Mike Müller
Mike Müller

Reputation: 85522

This should do:

from __future__ import print_function

import csv
import sys

if sys.version_info.major < 3:
    input = raw_input

input_gender = 'Boy'  # could make this a user-supplied value too

input_year = input('Please enter the year: ').strip()
file_name = 'babyQldAll.csv'

with open(file_name) as fobj:
    csv_file = csv.reader(fobj)
    name_counts = {}
    for row in csv_file:
        year, name, count, gender = (entry.strip() for entry in row)
        if year == input_year and gender == input_gender:
            name_counts[name] = int(count)

print('Popular {} names in year {} are:'.format(input_gender.lower(), input_year))
for name, count in name_counts.items():
    print(name, count)

print('\nBoys names in alphabetical order:')
for name in sorted(name_counts.keys()):
    print(name)

print('\nBoys names in alphabetical order and counts:')
for name, count in sorted(name_counts.items()):
    print('{}: {}'.format(name, count))

Some notes:

  1. In Python 2 you should use raw_input, which turned into input in Python 3. One way would to make your program work in same way in Python 2 and 3 would adding this to the beginning of your file:

    import sys
    
    if sys.version_info.major < 3:
        input = raw_input
    

    Now you can use input no matter what Python version.

  2. Your input has a space after the comma. Since the csv reader only looks for commas, the space is part of the string in your list for a row. Stripping it off with (entry.strip() for entry in row) solves this.

  3. So fare I compare a string for the year. I might be worthwhile to convert both, input_year and year to and int and compare these.

Running th Program with the example file in the question looks like thi:

Please enter the year: 2010
Popular boy names in year 2010 are:
Tom 111
Steven 212

Boys names in alphabetical order:
Steven
Tom

Boys names in alphabetical order and counts:
Steven: 212
Tom: 111

Upvotes: 2

Maslab Droid
Maslab Droid

Reputation: 11

dict[] is used to retrieve values stored in the dictionary using the key. Therefore, if you had the above stored as:

names = {'Steven': [212, 'Boy', 2010], 'Mary': [423, 'Girl', 2010]}

And wanted to retrieve Steven's information, you would say names['Steven'], which would return the list [212, 'Boy', 2010].

I don't think this will fully answer your question, but it should help you get started. You may also want to look at this if you want to pre-sort your dictionary.

Upvotes: 1

Related Questions