HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Print distinct car names with count in descending order

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
distinctordercardescendingwithnamesprintcount

Problem

After reading this question, it jumped to me that many said that the solution to the problem is over-engineered / overkill.

Here I solve the same problem in Python in a more compact way. I think that this solution is compact enough. If you still think that this is overkill for the problem, please tell me. Any kind of other code review is welcome too.

"""
Item list analyzer

Given a file with an element in each line,
for each item this programme will print the number of times
it appears in the text file. It will also sort the items
from the most frequent to the least frequent.

Example:
    example.txt:
        spam \n spam \n eggs \n spam \n foo \n baz \n eggs
        # "\n" should be actual newlines not the "\n" string

    Ouput:
        ('spam', 3)
        ('eggs', 2)
        ('baz', 1)
        ('foo', 1)

"""
import operator

FILENAME = "cars.txt"

def return_frequncy_dict(lst):
    """
    Given a list as input returns a 
    dict containg the items as keys and
    the number of occurencies of each item as
    values.

    >>> return_frequncy_dict(["a","b","c","b","b","a"])
    {'a': 2, 'c': 1, 'b': 3}
    """
    return {i:lst.count(i) for i in lst}

def sort_by_value(dict_):
    """
    As the frequency is the value, to sort by frequency we
    must sort by value. Standard sorting goes from the smallest
    to the biggest, we want the reverse of it.

    Credit for this function goes to
    http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
    """
    return reversed(sorted(dict_.items(), key=operator.itemgetter(1)))

if __name__ == "__main__":
    with open(FILENAME,"r") as f:
        data = f.read()

    item_list = data.splitlines()
    item_list = [item.lower() for item in item_list]
    item_list = [item.strip() for item in item_list]

    for j in sort_by_value(return_frequncy_dict(item_list)):
        print(j)

Solution

I'm sure that someone who knows more Python than me will be able to provide more feedback, but I see a couple of issues.

The first is that the core of this functionality is implemented by collections.Counter. We can just write this:

def frequency(lst):
    counter = Counter(lst)
    return reversed(sorted(counter.items(), key=lambda x:x[1]))


The second issue is with these lines:

item_list = [item.lower() for item in item_list]
item_list = [item.strip() for item in item_list]


We can just write

item_list = [item.lower().strip() for item in item_list]


Or as @janos suggested,

item_list = [item.lower().strip() for item in data.splitlines()]

Code Snippets

def frequency(lst):
    counter = Counter(lst)
    return reversed(sorted(counter.items(), key=lambda x:x[1]))
item_list = [item.lower() for item in item_list]
item_list = [item.strip() for item in item_list]
item_list = [item.lower().strip() for item in item_list]
item_list = [item.lower().strip() for item in data.splitlines()]

Context

StackExchange Code Review Q#71448, answer score: 5

Revisions (0)

No revisions yet.