Last active
September 23, 2024 07:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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