Python - 用另一个列表替换字符列表

时间:2015-07-01 16:53:10

标签: python string unicode

我有两个清单:

wrong_chars = [
    ['أ','إ','ٱ','ٲ','ٳ','ٵ'],
    ['ٮ','ݕ','ݖ','ﭒ','ﭓ','ﭔ'],
    ['ڀ','ݐ','ݔ','ﭖ','ﭗ','ﭘ'],
    ['ٹ','ٺ','ٻ','ټ','ݓ','ﭞ'],
]

true_chars = [
    ['ا'],
    ['ب'],
    ['پ'],
    ['ت'],
]

对于给定的字符串,我想将wrong_chars中的条目替换为true_chars中的条目。在python中有没有一种干净的方法呢?

4 个答案:

答案 0 :(得分:7)

string模块救援!

作为名为string的{​​{1}}模块的一部分,有一个非常方便的功能,它完全符合您的要求,但您必须传入您的翻译映射为字典。

文档为here

基于tutoriapoint教程的示例如下所示:

translate

看起来你在这里使用的是unicode,但效果略有不同。您可以查看this question以获得理解,但这是一个应该更适合您的示例:

>>> from string import maketrans

>>> trantab = maketrans("aeiou", "12345")
>>> "this is string example....wow!!!".translate(trantab)

th3s 3s str3ng 2x1mpl2....w4w!!!

答案 1 :(得分:2)

我将你的两个错误和真实的字符合并在一个错误字典列表中,以及应该用它们替换的内容。所以你在这里:
链接到工作样本http://ideone.com/mz7E0R
和代码本身

given_string = "ayznobcyn"
correction_list = [
                    {"wrongs":['x','y','z'],"true":'x'},
                    {"wrongs":['m','n','o'],"true":'m'},
                    {"wrongs":['q','r','s','t'],"true":'q'}
                  ]

processed_string = ""
true_char = ""

for s in given_string:
    for correction in correction_list:
        true_char=s
        if s in correction['wrongs']:
            true_char=correction['true']
            break
    processed_string+=true_char

print given_string
print processed_string

这个代码可以更加优化,当然我不关心unicode问题,如果有的话,因为我看到你正在使用波斯语。你应该注意这一点。

答案 2 :(得分:1)

#!/usr/bin/env python
from __future__ import unicode_literals

wrong_chars = [
    ['1', '2', '3'],
    ['4', '5', '6'],
    ['7'],
]
true_chars = 'abc'

table = {}
for keys, value in zip(wrong_chars, true_chars):
    table.update(dict.fromkeys(map(ord, keys), value))
print("123456789".translate(table))

输出

aaabbbc89

答案 3 :(得分:0)

在我的想法中,您可以只创建一个包含真实字符的列表:

NewChars = {["ا"،"أ"،"إ"،"آ"], ["ب"،"بِ"،"بِ"،]} 
# add all true characters to the first of lists and add all lists to a dict, then:
Ch="إ"
For L in NewChars:
    If Ch in L: return L[0]