6
\$\begingroup\$

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)
asked Dec 2, 2014 at 21:51
\$\endgroup\$
0

2 Answers 2

5
\$\begingroup\$

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()]
answered Dec 3, 2014 at 1:21
\$\endgroup\$
0
1
\$\begingroup\$
reversed(sorted(dict_.items(), key=operator.itemgetter(1)))

can be written as a single function call:

sorted(dict_.items(), key=operator.itemgetter(1), reverse=True)
answered Dec 7, 2014 at 17:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.