In: Computer Science
Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Here is the required solution in python
here is the code
def fun(phrase):
#find unique words
originallist=list(phrase.split(" "))
uniqueword=set(originallist)
#create a empty list
lis=[]
#fill the entries in the list
for x in uniqueword:
frequency=0
for i in range(len(originallist)):
if x==originallist[i]:
frequency=frequency+1
lis.append([x,frequency])
#return the lis
return lis
#test case
phrase="Add a new function that takes a phrase as an argument and
counts each unique word in the phrase"
print(fun(phrase))