0

I need to take a string input and count the occurrence of a character only once per character(should not repeat for the same character) and print it in a list or set or tuple. But I'm not getting the desired result.

Code:

string1 = input('Enter your string: ')


for letter in string1:

    string2 = string1.count(letter,0)

    ss = str(string2)

    print(ss)

Output:

Enter your string: ggddg
3
3
2
2
3
6
  • 3
    And what is the desired output? Commented Jun 15, 2018 at 2:02
  • 3
    Explain more precisely what you need. Commented Jun 15, 2018 at 2:03
  • Possible duplicate of Count the number occurrences of a character in a string Commented Jun 15, 2018 at 2:56
  • It should print the number of occurrences of a letter only once and should print all of them in a list or tuple or set @OlivierMelançon Commented Jun 15, 2018 at 16:38
  • 1
    @OlivierMelançon Apologies, I will keep that in mind from now on. Commented Jun 15, 2018 at 18:06

6 Answers 6

3

Depending on how you want your output to look, you can use collections.Counter, which has a dict-like interface:

from collections import Counter

user_input = input('Enter your string: ')
counts = Counter(user_input)

print(counts)  # prints: Counter({'g': 3, 'd': 2})
Sign up to request clarification or add additional context in comments.

Comments

1

You are printing the count for each different character as many times as they appear in the string. First is g, there are 3 gs so you print 3, then another g comes and you count and print again. I think you want to print that there are 3 'g' and 2 'd'.

Try this:

letters = {}
for c in input("Enter your string: "):
    if c not in letters:
        letters[c] = 1
    else:
        letters[c] += 1

print(letters)

Output:

Enter your string: ggddg
{'g': 3, 'd': 2}

1 Comment

Most Understandable answer! Thank you :)
0

I think this is what you want, try it with the sample user string

#!python3

# sample user string: fdshjkfdskhjlfdsjklfdsevcxzm,.czxm,.

user_string = input('Enter your string: ')

all_letters = []
unique_letters = []

for letter in user_string:
    all_letters.append(letter)

for unique in all_letters:
    if unique in unique_letters:
        pass
    else:
        unique_letters.append(unique)

for item in unique_letters:
    print(item + ' = ' + str(all_letters.count(item)))

# results
'''
f = 4
d = 4
s = 4
h = 2
j = 3
k = 3
l = 2
e = 1
v = 1
c = 2
x = 2
z = 2
m = 2
, = 2
. = 2
'''

Comments

0

@Mahesh, counting occurrences of a character in a string can be efficiently done by using set and dictionary as follows.

Try it online at http://rextester.com/KQV63040.

Note: I have used json module to pretty print the dictionary.

You can use OrderedDict of collections module to retain the order of keys in dictionary based on insertion.

import json

string1 = input('Enter your string: ').strip()

unique_letters = set(string1)
letters_occurrences = {}

for letter in unique_letters:
    letters_occurrences[letter] = string1.count(letter)

print() # newline

# Pretty printing the dictionary
print(json.dumps(letters_occurrences, indent=4))

» Output

Enter your string: abbacddabbc

{
    "b": 4,
    "a": 3,
    "d": 2,
    "c": 2
}

Comments

0

Try this:

string1 = input('Enter your string: ')
print('\n'.join(list(set([i+' '+str(string1.count(i)) for i in string1]))))

Output:

Enter your string: aaabbcca
c 2
a 4
b 2

Or try this:

string1 = input('Enter your string: ')
print('\n'.join(list(set(map(lambda x: x+' '+str(string1.count(x)),string1)))))

Output:

Enter your string: aaabbcca
c 2
a 4
b 2

Comments

0

you could use a dict to storage that info

d = {} 

for c in string1:
    try:
        d[c] += 1
    except KeyError:
        d[c] = 1
print(d)

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.