I am trying to create a new dictionary from an english language dictionary that i have using PyEnchant.For example : the english word--> 'new' to become--> 'new':1e1n1w (i am counting the letters alphabetically) My problem is that i do not know how to access the objects of the english language dictionary so i can make a repetition structure and create the ne dictionary or change this one.(I have two files that make the dictionary , one FFT and one txt.)
Asked
Active
Viewed 159 times
1
-
do you want to know how to convert "new" to "1e1n1w" ? is that your question? – Jean-François Fabre Jan 01 '17 at 20:29
1 Answers
1
Start from a word and convert it to letter count / letter format, sorted alphabetically:
from collections import Counter
s = "new"
c = Counter(s)
print("".join(["{}{}".format(v,k) for k,v in sorted(c.items())]))
- count letters of the word using special
Counter
dictionary made for that purpose - create the new "word" using list comprehension by iterating on counter keys & values (sorted), recreating the string using
str.join
results in: 1e1n1w
One-liner to create the list from input list (here: 2 items) using listcomps:
newlist = ["".join(["{}{}".format(v,k) for k,v in sorted(Counter(word).items())]) for word in ['new','hello']]
results in:
['1e1n1w', '1e1h2l1o']

Jean-François Fabre
- 137,073
- 23
- 153
- 219
-
Theres no need for the for loop, `c = Counter(s)` would do the work. – hashcode55 Jan 01 '17 at 20:44