根据字典替换字符串的快速方法 [英] Fast way to replace a string according to dict

查看:66
本文介绍了根据字典替换字符串的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据字典替换一个很长的字符串.我的代码是这样的:

I want to replace a very long string according to dictionary. My code is like that:

def rep(self, mystr, dict):
    new_pstr = ''
    for char in mystr:
        try:
            new_pstr += dict[char]
        except:
            continue
    return new_pstr

这段代码没有我预期的那么快.也许没有循环,它会更快.但我不知道该怎么做.最后但并非最不重要的是,我不能将所有相同的字符替换在一起,每次只能替换一个字符.因此,替换功能可能不是我的选择.为了更清楚,我举个例子:

This code is not as fast as I expected. Maybe without loop, it will be more faster. But I don't know how to do it. Last but not least, I can't replace all same character together, each time I should replace only one character. Therefore, replace function may not be my option. To be more clear, I give an example:

如果 d = {'A':'C', 'C':'B'}mystr = 'AC',则 new_pstr= 'CB'

(如果你的方式返回我'BB',这不是我所期望的)

(If your way return me 'BB', which is not what I expected)

实际上,我的字典是这样的:

In actual case, my dictionary looks like that:

d = {u'q': [u'k'], u'v': [u'v'], u'e': [u'e'], u'\xe7': [u'\xe7'], u'\xe9': [u'e'], u'h': [u'y'], u'j': [u'z'], u'o': [u'u'], u'\xf1': [u'g'], u'i': [u'i'], u'\u015f': [u's'], u'\xf6': [u'u'], u'x': [u'x'], u'\xfc': [u'v'], u'\u011f': [u'g']}

和我的字符串一样:

<代码> STR =tériniñyiriklişipkétişihavadikinemlikniñtövenlepketkenlikidin bolup,BU vaqitta TEREtéximuqurğaqlişipkétidu,tériniñilastikiliqi acizlap,xünükbolup qalidu砂XAnim的 - .qizlar bundaq vaqitta TEREqurğaqlişişniñ阿尔迪尼alidiğan转交tedbirlerniqollinişkéreknemliknisaqlaştayuquri dericilikスtoluqlaşyüzlüki,黑塞尔已经örükméğiziméyiğamuvapiq miqdarda未arilaşturupmelhem qilipyüzgeçaplap的BER即,烧nemxuşluqiyuquribolğanTEREnemleştürüşvazilinméyisürüpberse,qurğaqtérige苏toluqlaşqapaydiliq.

我使用 try.. except ... 的原因是因为我的代码有时会返回这样的错误 UnicodeEncodeError: 'ascii' codec can't encode characteru'\xe7' 在位置 2:序号不在范围内(128)

推荐答案

我通过结合@Padraic Cunningham @PRVS 的答案得到最终答案,此版本比我的原始代码快 100 倍.

I get the final answer by combining the answers from @Padraic Cunningham @PRVS, this version is 100 times faster than my original code.

 new_d = {ord(k): ord(v[0]) for k, v in d.items()} # ord for Unicode characters
 mystr.translate(d)

如果您的代码没有任何 Unicode 字符,请查看@PRVS 答案.

If your code don't have any Unicode characters, please check @PRVS answer.

这篇关于根据字典替换字符串的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆