I'm counting some string which i'm getting from text file. I have done that already but I want to know is there any other way that i can find quickly. Below is my code:-
Here first I'm finding all the string and putting all these in a list. Then I'm making a list of unique query then after I'm using the count method to find the count.
input.txt
shoes
memory card
earphones
led bulb
mobile
earphones
led bulb
mobile
above is my input file.
new = []
with open("input.txt") as inf:
for line in inf:
line = line.strip("\n")
new.append(line)
unique = list(set(new))
for i in unique:
cnt = new.count(i)
print i,cnt
and output should look like this:
mobile 2
memory card 1
led bulb 2
shoes 1
earphones 2