Python Forum
Counting the number of letters in a string alphabetically
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting the number of letters in a string alphabetically
#1
This is what I have so far and it is able to print out how many times a letter was printed but in order which it shows up in the string. I want it to first categorize by the number of times it popped up like the example output: (Also mine counts spaces, punctuations, etc. too but I don't want that)
Sentence Statistics : e appeared 3 times . a appeared 2 times . g appeared 2 times . l appeared 2 times . o appeared 2 times . c appeared 1 times . d appeared 1 times . i appeared 1 times . m appeared 1 times . n appeared 1 times . t appeared 1 times . w appeared 1 times .
Yes with the space between >1 and 1.

This is what I have:
string = input("Enter a Sentence: ") print(f"\nSentence Statistics:") one = 0 from collections import Counter count = Counter(string) for i in string: if count[i] == 1 and one == 0: print(f"\n{i} appeared {count[i]} times.") one += 1 elif count[i] == 1 and one >= 1: print(f"{i} appeared {count[i]} times.") if count[i] > 1: print(f"{i} appeared {count[i]} times.")
Reply
#2
from collections import Counter import string test_string = 'Sentence Statistics' counter = Counter(test_string) for item, count in counter.most_common(): if item in string.ascii_letters: print(f'{item} appeared {count} times')
Output:
t appeared 4 times e appeared 3 times S appeared 2 times n appeared 2 times c appeared 2 times i appeared 2 times s appeared 2 times a appeared 1 times
Reply
#3
(Mar-06-2019, 10:01 PM)Yoriz Wrote:
from collections import Counter import string test_string = 'Sentence Statistics' counter = Counter(test_string) for item, count in counter.most_common(): if item in string.ascii_letters: print(f'{item} appeared {count} times')
Output:
t appeared 4 times e appeared 3 times S appeared 2 times n appeared 2 times c appeared 2 times i appeared 2 times s appeared 2 times a appeared 1 times

Nice. It's working like a charm. Just how do I alphabetize within the same amount of appearances? Also how do I make it case-insenstitive?
Here's the updated code:

from collections import Counter import string istring = input("Enter your sentence: ") counter = Counter(istring) one = 0 print(f"\nSentence Statistics:") for item, count in counter.most_common(): if item in string.ascii_letters: if count != 1: print(f'{item} appeared {count} times.') if count == 1 and one == 0: one += 1 print(f'\n{item} appeared {count} times.') elif count == 1 and one != 0: print(f'{item} appeared {count} times.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Extracting Version Number from a String britesc 2 4,192 May-31-2023, 10:20 AM
Last Post: britesc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 7,813 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 8,754 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 6,053 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 2,716 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Regular expression: cannot find 1st number in a string Pavel_47 2 3,679 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  counting vowels in a string project_science 3 9,047 Dec-30-2020, 06:44 PM
Last Post: buran
  Replacing a words' letters in a string cananb 2 5,303 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Counting Number of Element in a smart way quest 2 3,495 Nov-09-2020, 10:24 PM
Last Post: quest
  Please support regex for version number (digits and dots) from a string Tecuma 4 5,888 Aug-17-2020, 09:59 AM
Last Post: Tecuma

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.