Skip to content

Instantly share code, notes, and snippets.

@theseoriddler
Last active May 22, 2023 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theseoriddler/79035be75a2bac82078608cf89b78aa8 to your computer and use it in GitHub Desktop.
Save theseoriddler/79035be75a2bac82078608cf89b78aa8 to your computer and use it in GitHub Desktop.
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]
#print(line)
listOfWords += line.split()
x+=1
#print(listOfWords)
newlist = []
for z in range(len(listOfWords)):
word = listOfWords[z]
counter = listOfWords.count(word)
#print(counter)
st = word+" occurs "+str(counter)+ " times."
if not(st in newlist):
newlist.append(st)
print(st)
z+=1
print(newlist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment