473,473 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why "g".count('')==2 ?

my question is as title!
thanks!

Mar 11 '06 #1
5 1096
"ygao" wrote:
my question is as title!


my answer as code:
s = "g"
t = ""
s[0:0+len(t)] == t True s[1:1+len(t)] == t

True

</F>

Mar 11 '06 #2
On Sat, 11 Mar 2006 13:37:05 +0100, Fredrik Lundh wrote:
"ygao" wrote:
my question is as title!


my answer as code:
s = "g"
t = ""
s[0:0+len(t)] == t True s[1:1+len(t)] == t

True

Or in other words, imagine that Python is walking the string looking to
match the target. The empty string matches the boundary of every character
with the next character, or in other words, for a string s of length N,
s.count('') will equal N+1.

I'm not sure what to describe this surprising result as. It isn't a bug;
it isn't even really a gotcha. I guess the best description is that it is
just a surprising, but logical, result.

(Well, I was surprised -- but I can't fault the logic.)
--
Steven.

Mar 11 '06 #3
For the same reason as
"".count("") 1 "ab".count("")

3

This is counting slice positions, which is one more that the length of the
string.

Mar 11 '06 #4
Em Sáb, 2006-03-11 Ã*s 04:25 -0800, ygao escreveu:
my question is as title!
thanks!


Forget it. Just look:

$ python2.4 -mtimeit '"g".count("")'
1000000 loops, best of 3: 0.516 usec per loop
$ python2.4 -mtimeit 'len("g")+1'
1000000 loops, best of 3: 0.26 usec per loop
--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Mar 11 '06 #5
Steven D'Aprano wrote:
On Sat, 11 Mar 2006 13:37:05 +0100, Fredrik Lundh wrote:

"ygao" wrote:

my question is as title!


my answer as code:

>s = "g"
>t = ""
>s[0:0+len(t)] == t


True
>s[1:1+len(t)] == t


True


Or in other words, imagine that Python is walking the string looking to
match the target. The empty string matches the boundary of every character
with the next character, or in other words, for a string s of length N,
s.count('') will equal N+1.

I'm not sure what to describe this surprising result as. It isn't a bug;
it isn't even really a gotcha. I guess the best description is that it is
just a surprising, but logical, result.


Yes, but:

py> "gab".split("")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: empty separator

The idea is not consistent between string functions. I actually consider
this latter example a bug.

Jaems

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Mar 11 '06 #6

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

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
2
by: Julio Allegue | last post by:
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It returns all the rows. It doesn't seem to look at the WHERE clause. At the same time, I am getting the correct count on "SQL...
18
by: Martin Jørgensen | last post by:
Hi, Today I got a really strange problem... I've made myself a data-file and I read in data from that file.... When I read something like this line: 03 04 05, 00 04 01, 05 03 07, 08 03...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
32
by: vippstar | last post by:
Assuming all the values of int are in the range of unsigned char, what happends if getc returns EOF? Is it possible that EOF was the value of the byte read? Does that mean that code aiming for...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.