0
longtext=input()
x=longtext.split(" ")
freq=1
for i in range(0,(len(x)-1)):
    for j in range(i+1,len(x)):
    if(x[j]==x[i]):
        freq=freq+1
        print(x[i],freq)

input:hello world karteek karteek hello output: hello 2 karteek 3

akazuko
  • 1,394
  • 11
  • 19
  • Look at: https://stackoverflow.com/a/2162045/7321201 – nishant Apr 22 '20 at 03:12
  • 1
    Does this answer your question? [How to count the frequency of the elements in a list?](https://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-a-list) – nishant Apr 22 '20 at 03:13

1 Answers1

2

You can use defaultdict

from collections import defaultdict
d = defaultdict(int)
phrase = "hello world karteek karteek hello"
for word in phrase.split(" "):
    d[word] += 1
print(d)

defaultdict(<class 'int'>, {'hello': 2, 'world': 1, 'karteek': 2})
ivan_filho
  • 1,371
  • 1
  • 10
  • 13