String count with overlapping occurrences
Well, this might be faster since it does the comparing in C:
def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count+=1
else:
return count
Python: Count overlapping substring in a string
You could use a running index to fetch the next occurance:
bla = 'hannahannahskdjhannahannah'
cnt = 0
idx = 0
while True:
idx = bla.find('hannah', idx)
if idx >= 0:
cnt += 1
idx += 1
else:
break
print(cnt)
Gives:
>> 4
Overlapping count of substring in a string in Python
def sliding(a, n):
return (a[i:i+n] for i in xrange(len(a) - n + 1))
def substring_count(a, b):
return sum(s == b for s in sliding(a, len(b)))
assert list(sliding('abcde', 3)) == ['abc', 'bcd', 'cde']
assert substring_count('ababaa', 'aba') == 2
How can I match overlapping strings with regex?
You can't do this with a regex alone, but you can get pretty close:
var pat = /(?=(\d{3}))\d/g;var results = [];var match;
while ( (match = pat.exec( '1234567' ) ) != null ) { results.push( match[1] );}
console.log(results);
Python String count does not shift count?
You can use re
for example:
import re
a = "01010101"
print(len(re.findall(r"(?<=0)1(?=0)", a)))
Prints:
3
Count overlapping occurrances of substring within string
The implementation count1
below will avoid creating a std::string if you pass a char pointer or a C string.
The second implementation count2
is probably yet more efficient.
#include <iostream>
#include <cstdint>
#include<iostream>
std::size_t count1( const std::string_view& needle, const std::string_view& haystack )
{
std::size_t count = 0;
std::size_t n = needle.size();
for ( std::size_t j=0; j<haystack.size()-n+1; ++j ) {
if ( haystack.substr(j,n)==needle ) {
count += 1;
}
}
return count;
}
std::size_t count2( const std::string_view& needle, const std::string_view& haystack ) {
std::size_t count =0;
std:size_t pos = haystack.find( needle, 0 );
while ( pos != std::string_view::npos ) {
count += 1;
pos = haystack.find( needle, pos+1 );
}
return count;
}
int main ( int argc, char* argv[] )
{
std::cout << "Count:" << count1( "lol", argv[1] ) << std::endl;
return 0;
}
Code: https://godbolt.org/z/9GG1TPcaM
Input:
llpol
Output:
0
Related Topics
What's the Difference Between Lists and Tuples
How to Convert a .Py to .Exe For Python
How to Find the Duplicates in a List and Create Another List With Them
How to Use Itertools.Groupby()
How to Create a Text Input Box With Pygame
Using Global Variables Between Files
How to Overcome "Datetime.Datetime Not Json Serializable"
How Does Tuple Comparison Work in Python
How to Sort a List of Strings Numerically
Pretty-Print a Numpy Array Without Scientific Notation and With Given Precision
How to Pad a String With Zeroes
How to Execute a String Containing Python Code in Python
Installing Python Module Within Code
How to Get the Source Code of a Python Function
Pandas: How to Easily Share a Sample Dataframe Using Df.To_Dict()
Changing One Character in a String
Is There Any Pythonic Way to Combine Two Dicts (Adding Values For Keys That Appear in Both)