Easiest way to calculate number of character in string

P

P. Schmidt-Volkmar

Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal
 
C

claude.henchoz

should = 42
has = Zeile.count(';')
if has < should:
Zeile += ";"*(should - has)

cheers, claude
 
C

Carsten Haese

Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal

AnzahlSemikolon = Zeile.count(';')
if AnzahlSemikolon < 42:
Zeile += ';'*(42-AnzahlSemikolon)

HTH,

Carsten.
 
L

Lawrence Oluyede

Il 2005-12-21 said:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

One way can be this I think:

s = "your_string_full_of_;;;;;"
num_of_semicolons = len(s.split(";") - 1)
 
K

Kent Johnson

O

Ove Svensson

P. Schmidt-Volkmar said:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal

What about this:

Zaehler += ';'*max(0,42-Zaehler.count(';'))
 
O

Ove Svensson

Ove Svensson said:
P. Schmidt-Volkmar said:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal

What about this:

Zaehler += ';'*max(0,42-Zaehler.count(';'))

Sorry, should have been

Zeile += ';'*max(0,42-Zeile.count(';'))
 
B

Bengt Richter

Ove Svensson said:
P. Schmidt-Volkmar said:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal

What about this:

Zaehler += ';'*max(0,42-Zaehler.count(';'))

Sorry, should have been

Zeile += ';'*max(0,42-Zeile.count(';'))

I think You don't need the max
...
-3: ''
-2: ''
-1: ''
0: ''
1: ';'
2: ';;'
3: ';;;'

I.e.,

Zeile += ';'*(42-Zeile.count(';'))

should work, since a string is a sequence type and

http://docs.python.org/lib/typesseq.html

Says
"""
Operation Result Notes
...
s * n , n * s n shallow copies of s concatenated (2)
....
(2)
Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type as s). ...
"""

I guess it might be nice to mention this in help(str) also, to publish a useful fact better.
Maybe under str.__mul__ ?

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top