Wednesday, April 15, 2015

Python program that inputs a list of words, separated by white- space, and outputs how many times each word appears in the list

Write a Python program that inputs a list of words, separated by white- space, and outputs how many times each word appears in the list.



import collections

#without using Counter from Collections
def myFunc():
	myinp=input().split()
	mylist=list(set(myinp))
	mycount=[]
	for i in range(len(mylist)):
		mycount.append(myinp.count(mylist[i]))

	mydict=dict(zip(mylist,mycount))
	print (mydict)



#myFunc()

#Use Counter from Colections

def myFunc1():
	myinp=input().split()
	a=collections.Counter(myinp)
	print (dict(a))

myFunc1()
Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment