I will try to help you out.
First, the quality of the question could be better. I think you should try to provide some rationale to your snippet, that way anyone responding could help you by improving not only your solution, but also the thinking behind it (making you a better coder).
Second, this problem has already been solved with lots of good explanations around the web. Check out e.g. item frequency count in python which has a fantastic number of clever solutions.
Third, and this is perhaps the part that you expected, your question shows both that you need to improve your understanding of Python syntax, functionality and general algorithmic thinking. Let me elaborate.
Python Syntax and Functionality
Many of the other answers highlight that the way you are using fh
is not very good. Typically, you would have a multiline string written like this in Python:
fh = '''this is the first row
second row
third row'''
Other notable examples of where you would gain from having better understanding of the syntax and functionality of Python is e.g. list comprehension, builtin types and so on. Walk through the is the Python tutorial. Dig in to it, it is very useful!
Algorithmic Thinking
Usually, when I see code such as yours, I try to figure out if the problem is lack of understanding/knowledge of the tool (in this case Python), or of the solution itself. In you case, I think it is a bit of both.
When it comes to the solution, what many experienced coders do (or at least did in the beginning of their careers), is to work out how they intend to solve the problem. A typical experienced coder would check their toolbox to see what would work, and then apply it.
I would solve the problem by using a set use the set datatype to solve this problem. Sets are pretty convenient in most cases, and are well described in the area of discrete mathematics. Check out the book Discrete Mathematics and Its Applications by Rosen, which covers that and a whole bunch of other stuff that you will find useful in the future. (You can probably find a lot of online resources describing sets, but that book is the reference to the area of discrete mathematics. I recommend getting it.)
Because sets are so convenient, Python naturally has builtin support for it. Again, the Python tutorial comes to the rescue, check out the section on sets.
Possible Solution
So, what could a possible solution look like? I would do the following:
- Make sure that everything is in a multiline string.
- Split the string into a list.
- Create a set based on the list content.
- Convert back into a list so that it can be sorted.
- Sort.
- Print.
None of this requires any for
or if
statements, and uses a lot of the builtin functionality of the language.
Hope this advice will help you onwards!