-1

I'm making a function that gets the top dog names in NYC from a text file and prints the top 10.

Here is my code:

    for line in inputFile:
        fields = line.split("\t")
        nameList.append(fields[0])

    nameDict = {}

    for name in nameList:
        if name not in nameDict:
            nameDict[name] = 1
        else:
            nameDict[name] += 1

    keyList = []

    for name in nameDict:
        keyList.append(name)

    keyList.sort()

    for name in keyList:
        print(name, nameDict[name])

Here is an example of the text file:

dog_name gender breed birth dominant_color secondary_color third_color spayed_or_neutered guard_or_trained borough zip_code
Buddy M Afghan Hound Jan-00 BRINDLE BLACK n/a Yes No Manhattan 10003
Nicole F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021

Here is what happens now with my code:

"Keaton," 1
"Lady,Princess,Prescott" 1
"cathalina," 1
A. 1
A.A. 1
ABBA 1
ABBESS 1
ABBEYROAD 1
ABERCROMBIE 1
ABIGALE 1
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
skidaddle
  • 9
  • 3
  • 1
    You should use the standard library's Counter, which has a `most_common(n)` function: https://docs.python.org/3.7/library/collections.html#collections.Counter – Mark Dec 05 '19 at 01:31
  • 1
    Possible duplicate of https://stackoverflow.com/q/59167209/2284490 – Cireo Dec 05 '19 at 01:47
  • Variable and function names should follow the `lower_case_with_underscores` style. Also it looks like your data is tab-delimited, right? You could use the csv library to make reading it easier. – AMC Dec 05 '19 at 02:36

1 Answers1

-2

This might be overkill, but you could import into a pandas dataframe and use value_counts(). This would streamline the file IO a bit too.

Assuming Python 3x

import pandas as pd 

df = pd.read_csv(filename,delim_whitespace=True, index_col=False)
print(df['dog_name'].value_counts()[:10])

will print out the top 10 most popular dog names.

I made up some data:

dog_name gender breed birth dominant_color secondary_color third_color spayed_or_neutered guard_or_trained borough zip_code
Buddy M Afghan Hound Jan-00 BRINDLE BLACK n/a Yes No Manhattan 10003
Nicole F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Keaton F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Princess F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Keaton F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Prescott F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Prescott F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Prescott F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021
Prescott F Afghan Hound Jul-00 BLACK n/a n/a Yes No Manhattan 10021

and the above code gives

Prescott    4
Keaton      2
Nicole      1
Princess    1
Buddy       1
Name: dog_name, dtype: int64
zylatis
  • 448
  • 3
  • 14