-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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Roemerdt
  • 147
  • 4
  • 14
  • 1
    So what attempt have you made to implement that? – jonrsharpe Oct 12 '14 at 15:08
  • Well I do know there is a sort method, but I don't know how to implement that. – Roemerdt Oct 12 '14 at 15:21
  • 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? – jonrsharpe Oct 12 '14 at 15:22
  • I know I have to sort my numbers array, but I don't know how to print the corresponding character along with the sorted array – Roemerdt Oct 12 '14 at 15:27
  • 1
    You will have to find a way to combine the `symbols` and `numbers` arrays before sorting - I suggest `zip`. – jonrsharpe Oct 12 '14 at 15:29
  • 1
    Another way would be using a [dictionary](https://docs.python.org/3/library/stdtypes.html#mapping-types-dict), with the letter as keys. For example `d = {}; d['a'] = d.get('a', 0) + 1` ([get method](https://docs.python.org/3/library/stdtypes.html#dict.get)) – parchment Oct 12 '14 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? – Roemerdt Oct 12 '14 at 15:45
  • http://stackoverflow.com/q/23918852/3001761 – jonrsharpe Oct 12 '14 at 20:29
  • Thanks, but I already figured it out – Roemerdt Oct 12 '14 at 20:52

2 Answers2

1
for number, symbol in sorted(zip(numbers, symbols), reverse=True):
    if number:
        print(symbol, number)
Jim Garrison
  • 4,199
  • 4
  • 25
  • 39
  • 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 – Roemerdt Oct 12 '14 at 17:51
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.

Joop
  • 7,840
  • 9
  • 43
  • 58
  • 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) – Roemerdt Oct 13 '14 at 11:41
  • then you will agree that x.most_common() is much more readable – Joop Oct 13 '14 at 11:47
  • of course... "Dict subclass for counting hashable items" https://docs.python.org/2/library/collections.html – Joop Oct 13 '14 at 11:51