Skip to content

Instantly share code, notes, and snippets.

Last active May 22, 2023 22:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Code to open a file "duplicatelines.txt", count words, then lists how many time each word was repeated.
myfile = open("LearningPy/duplicatelines.txt","r") #replace with your file name
xString = myfile.readlines()
listOfWords = []
for x in range(len(xString)):
line = xString[x]
listOfWords += line.split()
newlist = []
for z in range(len(listOfWords)):
word = listOfWords[z]
counter = listOfWords.count(word)
st = word+" occurs "+str(counter)+ " times."
if not(st in newlist):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment