473,549 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count string replace occurances

if i have
mytext.replace( a,b)
how to find out many many occurances has been replaced?

Xah
xa*@xahlee.org
http://xahlee.org/

Jul 19 '05 #1
3 6243
On Sun, Jun 12, 2005 at 04:55:38PM -0700, Xah Lee wrote:
if i have
mytext.replace( a,b)
how to find out many many occurances has been replaced?


The count isn't returned by the replace method. You'll have to count
and then replace.

def count_replace(a , b, c):
count = a.count(b)
return count, s.replace(b, c)
count_replace(" a car and a carriage", "car", "bat")

(2, 'a bat and a batriage')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCrOQSJd0 1MZaTXX0RAm27AJ 47sF1yYVKYIboeS sIcAVQZuhaGPgCf RqRH
mdJtjbwypq/h7XcmVpZOfhs=
=9m3p
-----END PGP SIGNATURE-----

Jul 19 '05 #2
"Jeff Epler" wrote:
On Sun, Jun 12, 2005 at 04:55:38PM -0700, Xah Lee wrote:
if i have
mytext.replace( a,b)
how to find out many many occurances has been replaced?


The count isn't returned by the replace method. You'll have to count
and then replace.

def count_replace(a , b, c):
count = a.count(b)
return count, s.replace(b, c)
count_replace(" a car and a carriage", "car", "bat")

(2, 'a bat and a batriage')


I thought naively that scanning a long string twice would be almost
twice as slow compared to when counting was done along with replacing.
Although it can done with a single scan, it is almost 9-10 times
slower, mainly because of the function call overhead; the code is also
longer:

import re

def count_replace_s low(aString, old, new):
count = [0]
def counter(match):
count[0] += 1
return new
replaced = re.sub(old,coun ter,aString)
return count[0], replaced
A good example of trying to be smart and failing :)

George

Jul 19 '05 #3
Xah Lee <xa*@xahlee.org > wrote:
if i have
mytext.replace( a,b)
how to find out many many occurances has been replaced?


If 'a' and 'b' are different length,
- Count the string length, before and after. The difference should
be multiple of difference between length of 'a' and 'b'.

If they are same length,
- Split 'mytext', and count items.

--
William Park <op**********@y ahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
Jul 19 '05 #4

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

Similar topics

12
1826
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and then remove certain stopwords(like and, a, an, by, the etc) which is there in another txt file. Then, i need to run it thru a stemmer(a program...
4
1614
by: Jim Heavey | last post by:
I am wanting to find any instance in a string where more then on occurance of a value occurs and replace it with a single occurance of the value. Specifically I want to search a text field and find any time where more then a single ' (tic) occurs in succession, and replace it with a single ' (tic). Can this be done with a RegularExpression? ...
3
1356
by: Able | last post by:
Dear friends I have a large string with the character "*" spread randomly in the string. My goal is to search the string for the character "*" and retrieve the string with these removed. How is the best way to achieve this? Regards Able
24
2297
by: Derek Hart | last post by:
Is there an efficient line of code to count the number of instances of one string within another. If I have the sentence: "I want to go to the park, and then go home." It would give me a count of 2 for the word "go" Derek
8
8493
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? Y o O
3
2575
by: MB | last post by:
I need to replace all occurances of a certain character located between two tags. I have included an example of what i have come up with so far, but it doesn't work they way I want it to: str = "some content<script type=\"text/javascript\">var str = 'asdfasdfASDFASDF';<\/script>"; str = str.replace(/(<script.*?>.*?)a(.*?<\/script>)/gim,...
18
2404
by: Umesh | last post by:
Do you have any answer to it? thx.
2
3065
by: Smokey Grindle | last post by:
any fast way to do this? I just need to check if if a string which could have a lot of characters in it if it has numbers and count the number of numbers in the string. thanks!
5
9227
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of strings. Then, I googled and found this suggestion of some JavaScript Tutorials, so I used replace with a regex with a global switch:
0
7546
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7985
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6071
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5387
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.