【问题标题】:How to put the new keys that iv'e already found in the new string?如何将我已经找到的新键放入新字符串中?
【发布时间】:2021-10-18 19:52:23
【问题描述】:

我想输入我找到的新键并打印一个创建逻辑句子的新字符串:

words = {'h': 'e', 'k': 't', 'm': 'o', 'u': 'r', 'e': 'h', 't': 'k', 'o': 'm', 'r': 'u'}
def text(str):
    print('the original text', str)


original_text = 'puackich, hvhnkrally oaths phufhck. all ymr nhhd is pykemn. j.u.u.u kmltin.mmps iks nmk eio; ---> hkmu'


print(original_text)

if x == original_text:
    print(True)

else:


我该怎么办?

【问题讨论】:

    标签: string dictionary key


    【解决方案1】:

    如果要根据字典words 解码字符串,可以使用下一个示例str.join + dict.get。例如:

    words = {
        "h": "e",
        "k": "t",
        "m": "o",
        "u": "r",
        "e": "h",
        "t": "k",
        "o": "m",
        "r": "u",
    }
    
    
    def text(s):
        print("the original text", s)
        print("".join(words.get(ch, ch) for ch in s))
    
    
    original_text = "puackich, hvhnkrally oaths phufhck. all ymr nhhd is pykemn. j.u.u.u kmltin.mmps iks nmk eio; ---> hkmu"
    text(original_text)
    

    打印:

    the original text puackich, hvhnkrally oaths phufhck. all ymr nhhd is pykemn. j.u.u.u kmltin.mmps iks nmk eio; ---> hkmu
    practice, eventually makes perfect. all you need is python. j.r.r.r tolkin.oops its not him; ---> etor
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2011-05-22
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      相关资源
      最近更新 更多