translate letters to numbers using python

﹥>﹥吖頭↗ 提交于 2020-01-04 09:59:09

问题


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?


回答1:


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



回答2:


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.




回答3:


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'



回答4:


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(' ') ])


来源:https://stackoverflow.com/questions/13498882/translate-letters-to-numbers-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!