calvin
calvin

Reputation: 29

translate letters to numbers using python

What I have:

s='areyo uanap ppple'

What I want:

s='12345 12324 11123'

Should I use dictionary and translate each i in s.split(' ')? or is there a simpler method?

Upvotes: 2

Views: 565

Answers (4)

Rudolf Mühlbauer
Rudolf Mühlbauer

Reputation: 2531

if i understand the OP correctly, this might be a solution:

s='areyo uanap ppple'
def trans(word):
    d = {}
    for char in word:
        if char not in d.keys():
            d[char] = len(d.keys()) + 1
        yield str(d[char])
o = ' '.join([  ''.join(trans(word)) for word in s.split(' ')])
print repr(o)

which results in:

'12345 12324 11123'

building on the unique of unutbu answer, this would also be possible:

' '.join([''.join([ { a:str(i+1) for i,a in enumerate(unique(word)) }[char] for char in word]) for word in s.split(' ') ])

here is another one, i think i got a little carried away :)

' '.join([ w.translate(maketrans(*[ ''.join(x) for x in zip(*[ (a,str(i+1)) for i,a in enumerate(unique(w)) ]) ])) for w in s.split(' ') ])

Upvotes: 1

Darknight
Darknight

Reputation: 1152

s='areyo uanap ppple'
incr=1
out=''
dict={}
for x in s:
    if ' ' in x:
        incr=1
        dict={}
        out+=' '
        continue;
    if x in dict.keys():
        out+=str(dict[x])
        continue;

    out+=str(incr)
    dict[x]=incr
    incr=incr+1

print out //12345 12324 11123

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251051

using unique_everseen() from itertools recipes:

In [5]: def func(s):
    for x in s.split():
            dic={}
            for i,y in enumerate(unique_everseen(x)):
                     dic[y]=dic.get(y,i+1)
            yield "".join(str(dic[k]) for k in x)    
            dic={}
   ...:             

In [6]: " ".join(x for x in func('areyo uanap ppple'))
Out[6]: '12345 12324 11123'

In [7]: " ".join(x for x in func('abcde fghij ffabc'))
Out[7]: '12345 12345 11234'

Upvotes: 1

unutbu
unutbu

Reputation: 880299

You could use unicode.translate:

import string

def unique(seq): 
    # http://www.peterbe.com/plog/uniqifiers-benchmark (Dave Kirby)
    # Order preserving
    seen = set()
    return [x for x in seq if x not in seen and not seen.add(x)]

def word2num(word):
    uniqs = unique(word)
    assert len(uniqs) < 10
    d = dict(zip(map(ord,uniqs),
                 map(unicode,string.digits[1:])))
    return word.translate(d)

s = u'areyo uanap ppple'
for word in s.split():
    print(word2num(word))

yields

12345
12324
11123

Note that it is unclear what you want to happen if there are more than 9 unique letters in a word. I've used an assert to complain if word2num is passed such a word.

Upvotes: 1

Related Questions