4

I'm being asked to count the occurrence of letters and spaces and punctuation characters in a string from user input and and in the printout the letters should appear in order in which they appear in the text, but no letter should appear twice and also lowercase and uppercase should be counted as one. my code so far looks like this.

S = str(input("Enter a string: "))
S = S.lower()
alpha = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! "

for char in S:
     if char in alpha:
          count = S.count(char)
     print(char,':',count)

output

Enter a string: hello
h : 1
e : 1
l : 2
l : 2
o : 1

i want it to look like this

Enter a string: hello
    h : 1
    e : 1
    l : 2
    o : 1

i was thinking if i turned the string and the the count of occurred chars into a nested list like

Enter a string: hello
[['h', 1], ['e', 1], ['l', 2], ['l', 2], ['o', 1]]

could i remove the list that are the same and leave just one?

5 Answers 5

6
>>> s = 'hello'
>>> s = s.lower()
>>> print('\n'.join('{} : {}'.format(c, s.count(c)) for i, c in enumerate(s) if c in "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! " and c not in s[:i]))
h : 1
e : 1
l : 2
o : 1
Sign up to request clarification or add additional context in comments.

Comments

6

This is a classic situation for using Counter of the collections module:

from collections import Counter

S ='hello'
S = S.lower()
d = Counter(S)
for item, ct in d.items():
    print '%s occured %d times' % (item, ct)

2 Comments

so Counter is like a dictionary? Counters is a container in which you can store things. I'm not to familiar with them
Yes Counter is inherited from dict class. So, it's a special dictionary for maintaining counts.
1

Just iterate over a set

S = str(input("Enter a string: ")).lower()
alpha = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! "

for char in set(S):
    if char in alpha:
        print(char,':',S.count(char))

Comments

0

You can do:

S = 'hello'
S = S.lower()
alpha = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! "

seen=set()

for c in S:
    if c not in seen and c in alpha:
        print '{} : {}'.format(c, S.count(c))
        seen.add(c)

Prints:

h : 1
e : 1
l : 2
o : 1

Comments

0

so i figured out how to do it without using sets and counters.

S = (input("Enter a string: "))
S = S.lower()
alpha = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! "
L = []
#counts the occurrence of characters in a string.
#then puts them in a list
for char in S:
     if char in alpha:
          count = S.count(char)
     L.append(char)
     L.append(count)
#turns list into a nested list
L2 = []
i = 0
while i <len(L):
        L2.append(L[i:i+2])
        i += 2
#checks that the lists dont appear more than once 
L3 = []
for i in L2:
     if i not in L3:
          L3.append(i)

# print out the letters and count of the letter
for i,k in L3:
     print(i,k)

might be long but it works. would like your opinion on it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.