使用几个字符的翻译生成字符串,并映射到其他几个字符

2024-09-29 19:22:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python代码中遇到了一个相当棘手的问题。我环顾四周,没有发现有类似问题的人

我想生成字符串,将一些字符转换成几个不同的字符

我想用几个不同的字符来代替原来的字符。 我想做的是这样的:

text = "hi there"
translations = {"i":["b", "c"], "r":["e","f"]}
result = magicfunctionHere(text,translations)
print(result)
> [
    "hb there",
    "hc there",
    "hi theee",
    "hi thefe",
    "hb theee",
    "hb thefe",
    "hc theee",
    "hc thefe"
]

结果包含原始文本的任意组合,其中“i”和“r”分别替换为“b”和“c”,以及“e”和“f”

我不知道如何做到这一点,使用itertools和函数,如排列、乘积等

希望我说得够清楚,这是一个相当具体的问题! 谢谢你的帮助


Tags: 字符串代码texthcresulthi字符there
2条回答

一个简单的方法是分两步使用Nested Loop Construct(函数),如下所示:

def rearrange_characters(str_text, dict_translations):
    tmp_result = []
    for key, value in dict_translations.items():
        if key in str_text:
            for replacer in value:
                str_temp = str_text.replace(key, replacer, 1)
                if str_temp not in tmp_result:
                    tmp_result.append(str_temp)
    return tmp_result


def get_rearranged_characters(str_text, dict_translations):
    lst_result = rearrange_characters(str_text, dict_translations)
    str_joined = ','.join(lst_result)
    for str_part in lst_result:
        str_joined = "{},{}".format(str_joined, ','.join(rearrange_characters(str_part, dict_translations)))
    return set(str_joined.split(sep=","))


text            = "hi there"
translations    = {"i": ["b", "c"], "r":["e","f"]}
result          = get_rearranged_characters(text, translations)
print(result)
## YIELDS: {
            'hb theee', 
            'hc thefe', 
            'hc there', 
            'hi thefe', 
            'hb thefe', 
            'hi theee', 
            'hc theee', 
            'hb there'
            }

enter image description here

另见:https://eval.in/960803

另一种同样复杂的方法是使用单个函数和嵌套循环,如下所示:

def process_char_replacement(str_text, dict_translations):
    tmp_result = []
    for key, value in dict_translations.items():
        if key in str_text:
            for replacer in value:
                str_temp = str_text.replace(key, replacer, 1)
                if str_temp not in tmp_result:
                    tmp_result.append(str_temp)

    str_joined = ','.join(tmp_result)
    for str_part in tmp_result:
        tmp_result_2 = []
        for key, value in dict_translations.items():
            if key in str_part:
                for replacer in value:
                    str_temp = str_part.replace(key, replacer, 1)
                    if str_temp not in tmp_result_2:
                        tmp_result_2.append(str_temp)
        str_joined = "{},{}".format(str_joined, ','.join(tmp_result_2))
    return set(str_joined.split(sep=","))


text            = "hi there"
translations    = {"i": ["b", "c"], "r":["e","f"]}      
result          = process_char_replacement(text, translations)
print(result)
## YIELDS: {
            'hb theee', 
            'hc thefe', 
            'hc there', 
            'hi thefe', 
            'hb thefe', 
            'hi theee', 
            'hc theee', 
            'hb there'
            }

请参阅:https://eval.in/961602

def magicfunction(ret, text, alphabet_location, translations):
    if len(alphabet_location) == 0:
        ret.append(text)
        return ret
    index = alphabet_location.pop()
    for w in translations[text[index]]:
        ret = magicfunction(ret, text[:index] + w + text[index + 1:], alphabet_location, translations)
    alphabet_location.append(index)
    return ret

def magicfunctionHere(text, translations):
    alphabet_location = []
    for key in translations.keys():
        alphabet_location.append(text.find(key))
        translations[key].append(key)
    ret = []
    ret = magicfunction(ret, text, alphabet_location, translations)
    ret.pop()
    return ret

text = "hi there"
translations = {"i":["b", "c"], "r":["e","f"]}
result = magicfunctionHere(text,translations)
print(result)

相关问题 更多 >

    热门问题