+ 6
count(3,2) will start from 3 and increase by interval 2
3,5,7,9.....
4 Respuestas
+ 2
Use a generator.
Edit: Just saw 'itertools' tag. Here's the doc section:
https://docs.python.org/3/library/itertools.html#itertools.count
+ 2
Count is a string function used to find occurrence of any substring. Here it's basic synatax
count(sub, start= 0,end=len(string))
count ("i",4,30)
First argument is for sub string ..... second is starting point and third is ending point. see below example and hope it will help you
str = "this is simple string example ... "
sub = "i"
print ("str.count(sub) : ", str.count(sub))
#Above code will search for whole string to count 'a' in string are
#Output will be 4
print ("str.count(sub, 0, 30) : ", str.count(sub, 0, 30))
#Output will be 4
print ("str.count(sub, 4, 30) : ", str.count(sub, 4, 30))
#Output will be 3
sub = "w"
print ("str.count(sub) : ", str.count(sub))
#Output will be 0
+ 1
The currently upvoted answer isn't related to itertools so the vote load seems peculiar. Hopefully that doesn't burn you if you're answering a test question.
Note that itertools uses a generator as an inside example, but that example's still slower than count(). Best of luck.
0
here's what I think is a better explanation of count():
say you have a str or a list, and want to know how many of an "item" are in that str/list,
list.count("item",a,b)
where list is the name of the list, "item" is the item you're looking for, a is where to start looking from, and b is where to stop. Note that you don't necessarily need an a and b, you can write it like this : list.count("item")