Reputation: 15091
This is my solution resulting in an error. Returns 0
PS: I'd still love a fix to my code :)
from collections import Counter
import string
def count_letters(word):
global count
wordsList = string.split(word)
count = Counter()
for words in wordsList:
for letters in set(words):
return count[letters]
word = "The grey old fox is an idiot"
print count_letters(word)
Upvotes: 15
Views: 149825
Reputation: 1
string=str(input("Enter any sentence: "))
s=string.split()
a=0
b=0
for i in s:
a=len(i)
b=a+b
print(b)
It works perfectly without counting spaces of a string
Upvotes: 0
Reputation: 1
word_display = ""
for letter in word:
if letter in known:
word_display = "%s%s " % (word_display, letter)
else:
word_display = "%s_ " % word_display
return word_display
Upvotes: 0
Reputation: 1504
n=str(input("Enter word: ").replace(" ",""))
ans=0
for i in n:
ans=ans+1
print(ans)
Upvotes: 3
Reputation: 1
Try using...
resp = input("Hello, I am stuck in doors! What is the weather outside?")
print("You answered in", resp.ascii_letters, "letters!")
Didn't work for me but should work for some random guys.
Upvotes: -2
Reputation: 38165
def count_letter(string):
count = 0
for i in range(len(string)):
if string[i].isalpha():
count += 1
return count
print(count_letter('The grey old fox is an idiot.'))
Upvotes: 0
Reputation: 45
I found this is working perfectly
str = "count a character occurance"
str = str.replace(' ', '')
print (str)
print (len(str))
Upvotes: 0
Reputation: 41
Counting number of letters in a string using regex.
import re
s = 'The grey old fox is an idiot'
count = len(re.findall('[a-zA-Z]',s))
Upvotes: 4
Reputation: 41
I managed to condense it into two lines of code:
string = input("Enter your string\n")
print(len(string) - string.count(" "))
Upvotes: 4
Reputation: 1302
For another one-liner solution:
def count_letters(word): return len(filter(lambda x: x not in " ", word))
This works by using the filter function, which lets you pick the elements of a list that return true when passed to a boolean-valued function that you pass as the first argument. I'm using a lambda function to make a quick, throwaway function for that purpose.
>>> count_letters("This is a test")
11
You could easily extend this to exclude any selection of characters you like:
def count_letters(word, exclude): return len(filter(lambda x: x not in exclude, word))
>>> count_letters ("This is a test", "aeiou ")
7
Edit: However, you wanted to get your own code to work, so here are some thoughts. The first problem is that you weren't setting a list for the Counter object to count. However, since you're looking for the total number of letters, you need to join the words back together again rather than counting each word individually. Looping to add up the number of each letter isn't really necessary because you can pull out the list of values and use "sum" to add them.
Here's a version that's as close to your code as I could make it, without the loop:
from collections import Counter
import string
def count_letters(word):
wordsList = string.split(word)
count = Counter("".join(wordsList))
return sum(dict(count).values())
word = "The grey old fox is an idiot"
print count_letters(word)
Edit: In response to a comment asking why not to use a for loop, it's because it's not necessary, and in many cases using the many implicit ways to perform repetitive tasks in Python can be faster, simpler to read, and more memory-efficient.
For example, I could have written
joined_words = []
for curr_word in wordsList:
joined_words.extend(curr_word)
count = Counter(joined_words)
but in doing this I wind up allocating an extra array and executing a loop through the Python interpreter that my solution:
count = Counter("".join(wordsList))
would execute in a chunk of optimized, compiled C code. My solution isn't the only way to simplify that loop, but it's one way.
Upvotes: 3
Reputation: 123473
OK, if that's what you want, here's what I would do to fix your existing code:
from collections import Counter
def count_letters(words):
counter = Counter()
for word in words.split():
counter.update(word)
return sum(counter.itervalues())
words = "The grey old fox is an idiot"
print count_letters(words) # 22
If you don't want to count certain non-whitespace characters, then you'll need to remove them -- inside the for
loop if not sooner.
Upvotes: 3
Reputation: 5629
Simply solution using the sum function:
sum(c != ' ' for c in word)
It's a memory efficient solution because it uses a generator rather than creating a temporary list and then calculating the sum of it.
It's worth to mention that c != ' '
returns True or False
, which is a value of type bool
, but bool
is a subtype of int
, so you can sum up bool values (True
corresponds to 1
and False
corresponds to 0
)
You can check for an inheretance using the mro
method:
>>> bool.mro() # Method Resolution Order
[<type 'bool'>, <type 'int'>, <type 'object'>]
Here you see that bool
is a subtype of int
which is a subtype of object
.
Upvotes: 13
Reputation: 4961
def count_letters(word):
return len(word) - word.count(' ')
Alternatively, if you have multiple letters to ignore, you could filter the string:
def count_letters(word):
BAD_LETTERS = " "
return len([letter for letter in word if letter not in BAD_LETTERS])
Upvotes: 23
Reputation: 104742
MattBryant's answer is a good one, but if you want to exclude more types of letters than just spaces, it will get clunky. Here's a variation on your current code using Counter
that will work:
from collections import Counter
import string
def count_letters(word, valid_letters=string.ascii_letters):
count = Counter(word) # this counts all the letters, including invalid ones
return sum(count[letter] for letter in valid_letters) # add up valid letters
Example output:
>>> count_letters("The grey old fox is an idiot.") # the period will be ignored
22
Upvotes: 6