Python: How to check if a word appears more than once in a text. (No Sets)
In my assignment, I have a text and I want to count how many times that words appeared in the text. For example, let's say I have a text file saying.
I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!
Since the words dogs appeared 3 times, I need the output to be that number. However, how would I do this for a random text?
So far I've come up with the following.
file = open('phrases.txt')
text = file.read()
file.close()
count = countWords()
duplicates = 0
for words in text:
if words #appear twice or more
#if duplicates
duplicates+=1
unique = count - duplicates
#subtract the total, by the amount of duplicates.
print(unique)
countWords() is another function I made which counts the amount of total words inside the text**
python string python-3.x list file-io
add a comment |
In my assignment, I have a text and I want to count how many times that words appeared in the text. For example, let's say I have a text file saying.
I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!
Since the words dogs appeared 3 times, I need the output to be that number. However, how would I do this for a random text?
So far I've come up with the following.
file = open('phrases.txt')
text = file.read()
file.close()
count = countWords()
duplicates = 0
for words in text:
if words #appear twice or more
#if duplicates
duplicates+=1
unique = count - duplicates
#subtract the total, by the amount of duplicates.
print(unique)
countWords() is another function I made which counts the amount of total words inside the text**
python string python-3.x list file-io
1
dictionaries`? collections.Counter?
– Patrick Artner
Nov 12 '18 at 20:49
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. 5N particular, we expect you to research your question before posting here.
– Prune
Nov 12 '18 at 21:02
add a comment |
In my assignment, I have a text and I want to count how many times that words appeared in the text. For example, let's say I have a text file saying.
I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!
Since the words dogs appeared 3 times, I need the output to be that number. However, how would I do this for a random text?
So far I've come up with the following.
file = open('phrases.txt')
text = file.read()
file.close()
count = countWords()
duplicates = 0
for words in text:
if words #appear twice or more
#if duplicates
duplicates+=1
unique = count - duplicates
#subtract the total, by the amount of duplicates.
print(unique)
countWords() is another function I made which counts the amount of total words inside the text**
python string python-3.x list file-io
In my assignment, I have a text and I want to count how many times that words appeared in the text. For example, let's say I have a text file saying.
I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!
Since the words dogs appeared 3 times, I need the output to be that number. However, how would I do this for a random text?
So far I've come up with the following.
file = open('phrases.txt')
text = file.read()
file.close()
count = countWords()
duplicates = 0
for words in text:
if words #appear twice or more
#if duplicates
duplicates+=1
unique = count - duplicates
#subtract the total, by the amount of duplicates.
print(unique)
countWords() is another function I made which counts the amount of total words inside the text**
python string python-3.x list file-io
python string python-3.x list file-io
asked Nov 12 '18 at 20:42
AlegriaAlegria
12
12
1
dictionaries`? collections.Counter?
– Patrick Artner
Nov 12 '18 at 20:49
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. 5N particular, we expect you to research your question before posting here.
– Prune
Nov 12 '18 at 21:02
add a comment |
1
dictionaries`? collections.Counter?
– Patrick Artner
Nov 12 '18 at 20:49
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. 5N particular, we expect you to research your question before posting here.
– Prune
Nov 12 '18 at 21:02
1
1
dictionaries`? collections.Counter?
– Patrick Artner
Nov 12 '18 at 20:49
dictionaries`? collections.Counter?
– Patrick Artner
Nov 12 '18 at 20:49
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. 5N particular, we expect you to research your question before posting here.
– Prune
Nov 12 '18 at 21:02
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. 5N particular, we expect you to research your question before posting here.
– Prune
Nov 12 '18 at 21:02
add a comment |
2 Answers
2
active
oldest
votes
words = text.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
for k,v in counts.items() :
if v==1 :
print(k)
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
add a comment |
text = "I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!"
find = "dogs"
count = 0
for index, letter in enumerate(text):
if letter == find[0]:
word = text[index: index + len(find)]
if word == find:
count += 1
print(count)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269784%2fpython-how-to-check-if-a-word-appears-more-than-once-in-a-text-no-sets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
words = text.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
for k,v in counts.items() :
if v==1 :
print(k)
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
add a comment |
words = text.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
for k,v in counts.items() :
if v==1 :
print(k)
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
add a comment |
words = text.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
for k,v in counts.items() :
if v==1 :
print(k)
words = text.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
for k,v in counts.items() :
if v==1 :
print(k)
edited Nov 12 '18 at 20:50
answered Nov 12 '18 at 20:44
Sari MasriSari Masri
1297
1297
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
add a comment |
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
I'm getting KeyError at count[words]+=1
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
Also can you explain your code?
– Alegria
Nov 12 '18 at 23:14
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
I tried my code it works perfectly, the idea is you read lines from a file, and store it in array using split() to remove whitespace from a line ,then you make a dictionary and store the world in a key and the count in a value, so you have every world with its count ,and that it, in the last for loop you are printing the unique words which have count =1, please vote up and choose the question as best answer and if you have any problem please contact me
– Sari Masri
Nov 13 '18 at 5:58
add a comment |
text = "I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!"
find = "dogs"
count = 0
for index, letter in enumerate(text):
if letter == find[0]:
word = text[index: index + len(find)]
if word == find:
count += 1
print(count)
add a comment |
text = "I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!"
find = "dogs"
count = 0
for index, letter in enumerate(text):
if letter == find[0]:
word = text[index: index + len(find)]
if word == find:
count += 1
print(count)
add a comment |
text = "I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!"
find = "dogs"
count = 0
for index, letter in enumerate(text):
if letter == find[0]:
word = text[index: index + len(find)]
if word == find:
count += 1
print(count)
text = "I have lots of cats and dogs. I have 3 cats and 16 dogs. I love dogs!"
find = "dogs"
count = 0
for index, letter in enumerate(text):
if letter == find[0]:
word = text[index: index + len(find)]
if word == find:
count += 1
print(count)
answered Nov 12 '18 at 21:06
BaryonBaryon
365
365
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269784%2fpython-how-to-check-if-a-word-appears-more-than-once-in-a-text-no-sets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
dictionaries`? collections.Counter?
– Patrick Artner
Nov 12 '18 at 20:49
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. 5N particular, we expect you to research your question before posting here.
– Prune
Nov 12 '18 at 21:02