-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_string_freq.py
47 lines (39 loc) · 1.28 KB
/
sort_string_freq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import collections
import string
word = "aacbbcabcddaaababcdzabzdcdddddddddddd"
#--------------------------------------------------------------------------
dict = {c: 0 for c in word}
print (dict)
for c in word:
dict[c] += 1
print dict
#not sorted, need sorted string, below
st = ""
for key,val in dict.iteritems():
st = st + key + str(val)
print (st)
#----------------------------------------------------------------------------
d = collections.defaultdict(int)
print (d)
for c in word:
d[c] += 1
print (d)
st2 = ""
for c in sorted(d, key=d.get, reverse=False):
print '%s %6d' % (c, d[c])
st2 = st2 + c
print ("st2", st2)
#above is the final solution, it took a sting, converted to dict, sorted by freq, convert it back to string
#----------------------------------------------------------------------------
#sorted_dict[c] = d[c]
#sorted_dict = {c:d[c] for c in sorted(d, key=d.get, reverse=True) }
#print (sorted_dict)
#----------------------------------------------------------------------------
char_counter = collections.Counter(word)
print (char_counter)
for key in char_counter:
print key
for char, count in char_counter.most_common():
if char in word:
print(char, count)
#-----------------------------------------------------------------------------