0

I'm fairly new to Python and still have trouble displaying data that I have in the way that I want. I have this code that determines the most frequent character in a string. However, how I have it prints it as such: ('A', 3).

stringToData = raw_input("Please enter your string: ")
import collections
print (collections.Counter(stringToData).most_common(1)[0])

I just wanted some insight into how to manipulate this code to something similar to this:

print "In your string, there are: %s vowels and %s consonants." % (vowels, cons)

Obviously it would say, "In your string, the most frequent character is (character), which occurred (number) times."

I am using Python 2.7, and I tried using the pprint but I didn't really understand how to incorporate that into my existing code.

Edit: Basically, what I am asking is how I can code finding the most frequent character in a string and printing it in a manner such as "In your string, the most frequent character is (character), which occurred (number) times."

bij0ux
  • 55
  • 3
  • 8
  • 1
    What exactly are you hoping for `pprint` to do for you here? All it does is tweak the way large collections get printed out; you're not trying to display the collections at all. – abarnert Nov 08 '13 at 00:51

2 Answers2

4

I'm not sure if this is what you want but this will print the most frequent character followed with the number of occurrences:

import collections

char, num = collections.Counter(stringToData).most_common(1)[0]
print "In your string, the most frequent character is %s, which occurred %d times" % (char, num)

This returns a tuple of the most frequent character and the number of occurrences.

collections.Counter(stringToData).most_common(1)[0]
#output: for example: ('f', 5)

Example:

stringToData = "aaa bbb ffffffff eeeee"
char, num = collections.Counter(stringToData).most_common(1)[0]
print "In your string, the most frequent character is %s, which occurred %d times" % (char, num)

Output is:

In your string, the most frequent character is f, which occurred 8 times
1

There's really nothing for pprint to do here. That module is about customizing the way collections are printed out—indenting sub-objects, controlling the order in which dictionary keys or set elements get displayed, etc. You're not trying to print a collection at all, just print some information about it.

The first thing you want to do is keep the collection around instead of rebuilding it for each print statement:

counter = collections.Counter(stringToData)

Next, you have to figure out how to get the data you want out of it. You already know how to find one pair of values:

letter, count = counter.most_common(1)[0]

The other thing you've asked about is the count of vowels and consonants. For that, you'll want to do something like this:

all_vowel = set('aeiouyAEIOUY')
all_consonants = set(string.ascii_letters) - all_vowels
vowels = sum(count for letter, count in counter.iteritems()
             if letter in all_vowels)
cons = sum(count for letter, count in counter.iteritems()
           if letter in all_consonants)

And now you just need to print them out by using some kind of formatting, which you already know how to do:

print "In your string, there are: %s vowels and %s consonants." % (vowels, cons)
print ("In your string, the most frequent character is %s, which occurred %s times."
       % (letter, count))
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • The vowels and cons part is part of a different code which I already completed. I was just providing an example of what I wanted it to look like. – bij0ux Nov 08 '13 at 01:06