# string - Python character frequncy in decending order, if similar based on aphabets

how to order charters of a descending order? if similar apathetically.

frequency first then alphabetical series .

``#!/user/bin/python3# -*- coding: utf-8 -*-import stringtext = """Mississippi"""# create a character: frequency dictionarycharFreqDic = {}for char in text.upper():charFreqDic[char] = charFreqDic.get(char, 0) + 1print ("Characters sorted by ASCII number:")# create a sorted list of keyskey_list = sorted(charFreqDic.keys())for key in key_list:# don't show space and newlineif key not in " \n":# associate the value with the keyprint ("%2s %3d" % (key, charFreqDic[key]))print ("Characters sorted by frequency:")print("\n")print("No Occur"+"\t "+ "Character")# convert charFreqDic to list of (k, v) tuples with charFreqDic.items()# flip tuple elements to (v, k) using list comprehension# then sort list of tuples (order is v,k), highest v first#dictionary to listcopytolist=[]for K,V in charFreqDic.items():copytolist.append((V,K));copytolist=sorted(copytolist,reverse=True)print(copytolist)print("Words sorted by frequency:")print("\n")print("No Occur" + "\t " + "Word")for K in copytolist:if K[1] not in " \n":print(str(K[0]) + "\t\t\t " + str(K[1]) + "'s")``

output is

Words sorted by frequency:

``No Occur Word4 S's4 I's2 P's1 M's``

But I wanted the output to be ordered both based on frequency and based on alphabetical series if frequency is similar.

``No Occur Word4 I's4 S's2 P's1 M's``