473,378 Members | 1,436 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,378 software developers and data experts.

counting a collection of letters within a string

Hi
I am trying to write code that will return all instances of a collection of letters in a string variable...
I can get a count returned, but it does not cater for letters that are used twice... for example

Expand|Select|Wrap|Line Numbers
  1. s = 'ajsfhbababnsnbabnndbab'
  2. n = s .count (str('bab'))
  3. print 'Number of times bab occurs is: ' + str(n)
  4.  
The code returns 3 instances but actually it is 4 - it is not counting the babab as 2 instances.

Any suggestions gratefully received!
Thanks!
Jun 20 '14 #1
3 1254
dwblas
626 Expert 512MB
You have to create your own code when builtins assume things you don't want.
Expand|Select|Wrap|Line Numbers
  1. s = 'ajsfhbababnsnbabnndbab'
  2. found=0
  3. location = -1
  4. while True:
  5.     location = s.find("bab", location+1)
  6.     if location > -1:
  7.         found += 1
  8.     else:
  9.         break
  10.  
  11. print 'Number of times bab occurs is: %s' % (found) 
Jun 20 '14 #2
bvdet
2,851 Expert Mod 2GB
Create your own count function using string slice. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> def count_str(s, find_str):
  2. ...     count = 0
  3. ...     for i in range(len(s)):
  4. ...         if s[i:i+len(find_str)] == find_str:
  5. ...             count += 1
  6. ...     return count
  7. ... 
  8. >>> s = 'ajsfhbababnsnbabnndbab'
  9. >>> count_str(s, "bab")
  10. 4
  11. >>> 
Jun 20 '14 #3
bvdet
2,851 Expert Mod 2GB
Here's the function in my previous post as a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'ajsfhbababnsnbabnndbab'
  2. >>> find_str = "bab"
  3. >>> [s[i:i+len(find_str)] == find_str for i in range(len(s))].count(True)
  4. 4
  5. >>> 
Jun 22 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: colm | last post by:
I have a string surname and want to select the first two letters to use in a code field how can i check if the second item in string is an apostrophe and filter it out ' in names such as o'brien...
1
by: Shafia | last post by:
Hi, I need to refresh the Rows collection of my GridView within a tree event OnCheckChanged. I call the following for that void OnCheckChanged(....) { ...................
24
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...
4
by: John | last post by:
1. Is there an easy function in VB to count for instance the number of A's in a string? 2. Is there an easy funtion in VB to pull all characters out of a string into an array? (I tried the split...
8
by: aboon | last post by:
Hi, I really need you guys' help with getting string within string. I have an address validation code that will send a request string to UPS, and if the address is validated, UPS will send a...
1
by: nivedita prasad | last post by:
hi Occurrence of letters in String. Get string from KB of any length & print letters coming maximum time first than second largest….. i.e in descending order. program should be implemented...
6
by: peridian | last post by:
I've got some odd behaviour in a foreach loop, could anybody explain why this occurs? Simple class: public class ImageBindingObject { public ImageInfo ImageInfo { get; set;...
2
by: Udara chathuranga | last post by:
I need to use Sinhalese language Unicode letters within my personal web site. How can I do that ? Are their any special tags to insert Unicode letters? Final result should be like this web page....
2
by: Maris Neteiksu | last post by:
Hello, I have a variable string, named "line", a person writes some random stuff in it, and I need to count how many letters are in his text, how many numbers, and then how many other symbols. I have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.