回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>给定多字符到单字符字符串的转换表:</p>
<pre><code>>>> telex_mappings
{'eer': 'ể', 'awj': 'ặ', 'Dd': 'Đ', 'Ooj': 'Ộ', 'oox': 'ỗ', 'Aas': 'Ấ', 'Eej': 'Ệ', 'awx': 'ẵ', 'eef': 'ề', 'oo': 'ô', 'aas': 'ấ', 'Aax': 'Ẫ', 'owr': 'ở', 'Uws': 'Ớ', 'Awj': 'Ặ', 'Oor': 'Ổ', 'Awf': 'Ằ', 'ows': 'ớ', 'aaj': 'ậ', 'Owj': 'Ợ', 'aa': 'â', 'eex': 'ễ', 'Oox': 'Ỗ', 'Owr': 'Ở', 'awf': 'ằ', 'Aa': 'Â', 'aw': 'ă', 'awr': 'ẳ', 'uwj': 'ợ', 'uwx': 'ỡ', 'owj': 'ợ', 'Awx': 'Ẵ', 'Uwr': 'Ở', 'Aaj': 'Ậ', 'Eex': 'Ễ', 'Awr': 'Ẳ', 'Uw': 'Ư', 'Eef': 'Ề', 'aaf': 'ầ', 'Aws': 'Ắ', 'ees': 'ế', 'Ee': 'Ê', 'Ow': 'Ơ', 'Ees': 'Ế', 'Owx': 'Ỡ', 'Eer': 'Ể', 'Aar': 'Ẩ', 'Oo': 'Ô', 'uwf': 'ờ', 'uw': 'ư', 'uws': 'ớ', 'owx': 'ỡ', 'ow': 'ơ', 'aar': 'ẩ', 'eej': 'ệ', 'oof': 'ờ', 'ee': 'ê', 'uwr': 'ở', 'Aw': 'Ă', 'ooj': 'ộ', 'Aaf': 'Ầ', 'aax': 'ẫ', 'Oof': 'Ờ', 'oor': 'ổ', 'aws': 'ắ', 'Oos': 'Ớ', 'Uwf': 'Ờ', 'Uwx': 'Ỡ', 'dd': 'đ', 'oos': 'ố', 'Uwj': 'Ợ'}
>>> telex_mappings['eef']
'ề'
>>> telex_mappings['aaf']
'ầ'
>>> telex_mappings['uw']
'ư'
</code></pre>
<p>以及输入字符串:</p>
<pre><code>>>> s = 'Nguyeefn Traafn Anh Thuw'
</code></pre>
<p>所需功能如下:</p>
<pre><code>>>> func('Nguyeefn Traafn Anh Thuw')
'Nguyên Trân Anh Thư'
</code></pre>
<p>我试过:</p>
<pre><code>>>> s = 'Nguyeefn Traafn Anh Thuw'
>>> for k,v in telex_mappings.items():
... s = s.replace(k, v)
...
>>> s
'Nguyền Trâfn Anh Thư'
</code></pre>
<p>但是我们看到有一个“aa”的键,它使替换首先发生在“aaf”之前。理想情况下,应该先更换较长的钥匙。你知道吗</p>
<p><strong>替换应该如何使得可以从最长的键开始替换键值表中的子字符串?</strong></p>
<p><strong>是否存在某种regex方法,而不是遍历映射中的所有键值对?</strong></p>