How can I write a program that counts letters, numbers and punctuation(separately) in a string?
Asked
Active
Viewed 1.9k times
5 Answers
8
For a slightly more condensed / faster version, there is also
count = lambda l1,l2: sum([1 for x in l1 if x in l2])
so for example:
count = lambda l1,l2: sum([1 for x in l1 if x in l2])
In [11]: s = 'abcd!!!'
In [12]: count(s,set(string.punctuation))
Out[12]: 3
using a set should get you a speed boost somewhat.
also depending on the size of the string I think you should get a memory benefit over the filter as well.

Eiyrioü von Kauyf
- 4,481
- 7
- 32
- 41
-
I like this solution better. The top answer is a little more muddled by lambdas. – bozdoz May 27 '13 at 22:26
-
You don't need a list comprehension here, a plain generator expression is not only shorter but also more efficient. – Frerich Raabe Mar 12 '15 at 14:28
-
but not faster. generators are slower because they ... generate. hence why i said faster – Eiyrioü von Kauyf Mar 14 '15 at 06:38
-
even more concisely you could generate a regex for punctuation/numbers/letters - it would be faster than anything else here (depending on how they implemented the regex) – Eiyrioü von Kauyf Mar 14 '15 at 06:42
4
import string
a = "I'm not gonna post my homework as question on OS again, I'm not gonna..."
count = lambda l1, l2: len(list(filter(lambda c: c in l2, l1)))
a_chars = count(a, string.ascii_letters)
a_punct = count(a, string.punctuation)

BrainStorm
- 2,036
- 1
- 16
- 23
-
1`sum` should be `len` I think. And also, there is parenthesis mismatch. – utdemir Aug 06 '11 at 23:44
-
you're right, i wrote in about 10 seconds 'cause i was in a hurry, fixed =P – BrainStorm Aug 07 '11 at 06:28
-
Also a note, in Python 3+, filter returns a filter object and it doesn't support len. Maybe `list(filter(...))` is a better for compatibility. – utdemir Aug 07 '11 at 10:48
-
@utdemir nice to know, I'm still too lazy to install and use Py3 but i know i have to ^^' – BrainStorm Aug 07 '11 at 22:01
2
count_chars = ".arPZ"
string = "Phillip S. is doing a really good job."
counts = tuple(string.count(c) for c in count_chars)
print counts
(2, 2, 1, 1, 0)

Niklas R
- 16,299
- 28
- 108
- 203
1
>>> import string
>>> import operator
>>> import functools
>>> a = "This, is an example string. 42 is the best number!"
>>> letters = string.ascii_letters
>>> digits = string.digits
>>> punctuation = string.punctuation
>>> letter_count = len(filter(functools.partial(operator.contains, letters), a))
>>> letter_count
36
>>> digit_count = len(filter(functools.partial(operator.contains, digits), a))
>>> digit_count
2
>>> punctuation_count = len(filter(functools.partial(operator.contains, punctuation), a))
>>> punctuation_count
3
http://docs.python.org/library/string.html
http://docs.python.org/library/operator.html#operator.contains
http://docs.python.org/library/functools.html#functools.partial

utdemir
- 26,532
- 10
- 62
- 81
0
To loop over a string you can use a for-loop:
for c in "this is a test string with punctuation ,.;!":
print c
outputs:
t
h
i
s
...
Now, all you have to do is count the occurrences...

Fredrik Pihl
- 44,604
- 7
- 83
- 130