473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

string.count issue (i'm stupid?)

Hi all,

i've noticed a strange beaviour of string.count:

in my mind this code must work in this way:

str = "a_a_a_a_"
howmuch = str.count("_a_")
print howmuch -> 3

but the count return only 2

Ok this can be fine, but why? The doc string tell that count will
return the number of substring in the master string, if we spoke about
substring i count 3 substring...

Can someone explain me this? And in which way i can count all the
occurrence of a substring in a master string? (yes all occurrence
reusing already counter character if needed)

Thanks a lot

Matteo Rattotti www.rknet.it
Powered by:
- MacOsX
- Gnu / Linux Debian Sarge
- Amiga Os 3.9
- Milk

May 22 '06 #1
7 3453
I think I can tell you WHY this happens, but I don't know a work-around
at the moment.
It seems as if only the following "_a_" (A) are counted: a_A_a_A_

regards
Dirk

May 22 '06 #2
Matteo Rattotti wrote:
Hi all,

i've noticed a strange beaviour of string.count:

in my mind this code must work in this way:

str = "a_a_a_a_"
howmuch = str.count("_a_")
print howmuch -> 3

but the count return only 2

Ok this can be fine, but why? The doc string tell that count will
return the number of substring in the master string, if we spoke about
substring i count 3 substring...


It appears to be a documentation bug. It should say something about
non-overlapping occurences.

Better would be of course to introduce a parameter that defines the
behavior, either overlapping or not.

Diez

May 22 '06 #3
Le 22-05-2006, Matteo <ma*************@gmail.com> nous disait:
Hi all,

i've noticed a strange beaviour of string.count:

in my mind this code must work in this way:

str = "a_a_a_a_"
howmuch = str.count("_a_")
print howmuch -> 3

but the count return only 2

Ok this can be fine, but why? The doc string tell that count will
return the number of substring in the master string, if we spoke about
substring i count 3 substring...

Can someone explain me this? And in which way i can count all the
occurrence of a substring in a master string? (yes all occurrence
reusing already counter character if needed)


Use the optional start argument of find or index in a loop, such as:
def count_all(string, substring): .... index = 0
.... count = 0
.... while True:
.... index = string.find(substring, index)
.... if index < 0:
.... return count
.... else:
.... count += 1
.... index += 1
.... count_all("a_a_a_a_", '_a_')

3
--
Alexandre Fayolle LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
Python et calcul scientifique: http://www.logilab.fr/science
May 22 '06 #4
"Dirk Hagemann" <Di**********@gmail.com> writes:
I think I can tell you WHY this happens, but I don't know a work-around
at the moment.


len(re.findall('_(?=a_)', '_a_a_a_a_'))

# untested
def countWithOverlaps(s, pat):
return len(re.findall("%s(?=%s)" % (re.escape(pat[0]), re.escape(pat[1:])),s))

'as
May 22 '06 #5
Matteo Rattotti wrote:
Hi all,

i've noticed a strange beaviour of string.count:

in my mind this code must work in this way:

str = "a_a_a_a_"
dont use 'str' as an identifier, it shadows the builtin str type.
howmuch = str.count("_a_")
print howmuch -> 3

but the count return only 2

Ok this can be fine, but why? The doc string tell that count will
return the number of substring in the master string, if we spoke about
substring i count 3 substring...
depends on how you define "number of substring", I mean, overlapping or
not. FWIW, I agree that this may be somewhat unintuitive, and would at
least require a little bit more precision in the docstring.
Can someone explain me this?
It seems obvious that str.count counts non-overlapping substrings.
And in which way i can count all the
occurrence of a substring in a master string? (yes all occurrence
reusing already counter character if needed)


Look at the re module.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 22 '06 #6
I agree the docstring is a bit confusing and could be clarified
as to what's happening
Can someone explain me this? And in which way i can count all
the occurrence of a substring in a master string? (yes all
occurrence reusing already counter character if needed)

You should be able to use something like

s = "a_a_a_a_"
count = len([i for i in range(len(s)) if s.startswith("_a_", i)])

which will count the way you wanted, rather than the currently
existing count() behavior.

-tkc

May 22 '06 #7
We were doing something like this last week

thestring = "a_a_a_a_"
for x in range(len(thestring)):

.... try:
.... thestring.count("_a_", x, x + 3)
.... except ValueError:
.... pass

May 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
16
by: Christopher Benson-Manica | last post by:
I'm wondering about the best way to do the following: I have a string delimited by semicolons. The items delimited may be in any of the following formats: 1) 14 alphanum characters 2) 5...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
9
by: senfo | last post by:
I realize it's Friday and I'm probably already on vacation for the remainder of the day; but, I have a really, really stupid question. Is there a bug in the .NET 2.0 Framework in regards to the...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
1
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.