I was working on one problem while I encountered this problem. Let me explain through a simple example:
a = ['hello', 'world']
print(a.count('hello')) #prints 1 correctly
But...
a = "hello world"
for i in a.split():
print(a.count(i))
#prints
#2 -> for 'hello' which is wrong, one more than actual value
#1 -> counts of elements from 2nd onwards are correct
If I split
it before, it works perfectly:
a = 'hello world'
a = a.split()
for i in a:
print(a.count(i))
#correctly prints
1
1
So I think the issue only happens when I use the split
method directly in the for loop, but I'm not sure why.
Edit:
I rechecked it, and its printing wrong values for long statements, specifically for this one. Please check using this statement below:
"how many times does each word show up in this sentence word times each each word"
Edit 2:
Even though I was splitting the string in for loop directly, it did't change the original string and I was using count method on original string inside the for loop, which caused the error and the 'how' to show up two times.
Thanks to the user Bobby, for correctly pointing out.
CodePudding user response:
It's works fine on my machine with Python 3.10.0. I get 1s in every example. Please provide more info like Python version, maybe we'll get the reason why it works like that on your machine.
CodePudding user response:
In your first example a
has never been updated. Even in the for loop, you split a
but that didn't update a
. You can verify this directly:
a = "hello world"
for i in a.split():
print(a)
print(a.count(i))
If you want the split string you need to save that to a variable like you did in the first example. You can do this within the for loop if you want:
a = "hello world"
for i in b:=a.split():
print(b.count(i))
CodePudding user response:
count
counts how many times the argument is found in a list. So for your first example:
a = ['hello', 'world']
print(a.count('hello'))
Hello is only in the list once.
a = ['hello', 'hello', 'world', 'hello', 'world']
print(a.count('hello')
Would return 3.
CodePudding user response:
Are you sure this code below outputs 2 1
? It is clear that it should output 1 1
. It might be something to do with your Python version.
a = "hello world" for i in a.split(): print(a.count(i)) #prints #2 -> for 'hello' which is wrong, one more than actual value #1 -> counts of elements from 2nd onwards are correct
CodePudding user response:
The function count
when used on a string iterates the entire string for any match. It doesn't care about "words".
"abc".count("ab") # 1
From the docs:
str.count(sub[, start[, end]])
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
In your first example you use count
on the string.
When used on lists however, it does not do partial matchign between the items:
["a", "b", "c"].count("ab") # 0
From the docs:
list.count(x)
Return the number of times x appears in the list.
This is what you do in your second example. You use count
on a list. This happens because str.split
returns a list.
So these are two different count methods you're using. One for strings (first example - a
is a string) and one for lists (second example - a
is a list returned from str.split
).