-3

I wrote some code just for the fun of it

symbols = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] numbers = [] counter = 0 print("Enter Name") name = input(">") if name: new_name = list(name) for x in range(0, len(symbols)): count_name = new_name.count(symbols[x]) numbers.append(count_name) if count_name: counter += 1 print("Character amount =", counter) for x in range(0, len(numbers)): if numbers[x]: print(symbols[x], "=", numbers[x]) else: print("Input = NULL") 

When you enter your name for example roemer

It prints out something like this:

Character amount = 4 e = 2 m = 1 o = 1 r = 2 

But I want it to sort based on which character occurs most frequently

9
  • 1
    So what attempt have you made to implement that? Commented Oct 12, 2014 at 15:08
  • 1
    So, you've... read the relevant documentation? Made an attempt to implement something that went wrong? Tried to fix that implementation to no avail? Or just dumped it on us? Commented Oct 12, 2014 at 15:22
  • 1
    You will have to find a way to combine the symbols and numbers arrays before sorting - I suggest zip. Commented Oct 12, 2014 at 15:29
  • 1
    Another way would be using a dictionary, with the letter as keys. For example d = {}; d['a'] = d.get('a', 0) + 1 (get method) Commented Oct 12, 2014 at 15:35
  • 1
    I've added this: total = dict(zip(symbols, numbers)). this gives my a dictionary, but I've searched the internet for a bit and it seems impossible to sort by value? Commented Oct 12, 2014 at 15:45

2 Answers 2

1
for number, symbol in sorted(zip(numbers, symbols), reverse=True): if number: print(symbol, number) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! Would you perhaps also know how to display the most frequent character and also if there are multiple highest characters like e = 3 and r = 3. So basically display all most frequent characters
0

If is not homework use the standard library Counter in collections provide the needed functionality

from collections import Counter x =Counter("roemer") x.most_common() >>> [('r', 2), ('e', 2), ('m', 1), ('o', 1)] 

output of most common is already sorted. can use other methods of Counter class of use list comprehension to get output in needed format.

3 Comments

It is not homework, but I already did it this way. sort_dic = dict(sorted(zip(symbols, numbers))) maxval = max(sort_dic.items(),key=operator.itemgetter(1))[1] max_keys = [k for k, v in sort_dic.items() if v == maxval] max_sort_keys = sorted(max_keys) try: if max_sort_keys[1]: print("Most frequent characters are", max_sort_keys) except IndexError: print("Most frequent character is", max_sort_keys) print("Character amount =", counter)
then you will agree that x.most_common() is much more readable
of course... "Dict subclass for counting hashable items" docs.python.org/2/library/collections.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.