简体   繁体   English

使用匹配键中的值更新字典中的字符串

[英]update string from a dictionary with the values from matching keys

def endcode(msg,secret_d):
    for ch in msg:
        for key,value in secret_d:
             if ch == key:
                 msg[ch] = value
    return msg
encode('CAN YOU READ THIS',{'A':'4','E':'3','T':'7','I':'1','S':'5'}) 

This is my code. 这是我的代码。 What I am trying to do here is for every characters in a string msg , the function should search in the dictionary and replace it with the mapping string if the character ch is a key in the dictionary secret_d . 我在这里尝试做的是对于字符串msg每个字符,如果字符ch是字典secret_d的键,该函数应该在字典中搜索并用映射字符串替换它。

If ch is not a key in secret_d than keep it unchanged. 如果ch不是secret_d的键,则secret_d保持不变。

For the example, the final result is should be 'C4N YOU R34D 7H15' 例如,最终结果应该是'C4N YOU R34D 7H15'

Your function name is endcode but you are calling encode . 您的函数名称是endcode但您正在调用encode

But more important, I'll give you a hint to what's going on. 但更重要的是,我会告诉你一些正在发生的事情。 This isn't going to totally work, but it's going to get you back on track. 这不会完全起作用,但是会让您回到正轨。

def endcode(msg,secret_d):
    newstr=""
    for ch in msg:
        for key,value in secret_d.iteritems():
            if ch == key:
                newstr=newstr+value
    print(msg)
endcode('CAN YOU READ THIS',{'A':'4','E':'3','T':'7','I':'1','S':'5'}) 

But if you want a complete answer, here is mine. 但如果你想要一个完整的答案, 是我的。

A few issues: 一些问题:

  • As rb612 pointed out, there's a typo in your function definition ("en d code") 正如rb612所指出的,您的函数定义中有一个错字(“ en d code”)
  • you are doing nothing with the return value of your function after calling it 调用它后,你对函数的返回值没有任何作用
  • msg[ch] is trying to assign items in a string, but that's not possible, strings are immutable. msg[ch]试图在字符串中分配项目,但这是不可能的,字符串是不可变的。 You'll have to build a new string. 您必须构建一个新字符串。 You cannot "update" it. 你无法“更新”它。
  • in order to iterate over (key, value) pairs of a dictionary d , you must iterate over d.items() . 为了迭代字典d (key, value)对,你必须迭代d.items() Iterating over d will iterate over the keys only. 遍历d将仅遍历键。

That being said, here's my suggestion how to write this: 话虽这么说,这是我的建议如何写这个:

>>> def encode(msg, replacers):
...     return ''.join([replacers.get(c, c) for c in msg])
... 
>>> result = encode('CAN YOU READ THIS',{'A':'4','E':'3','T':'7','I':'1','S':'5'})
>>> result
'C4N YOU R34D 7H15'

Things to note: 注意事项:

  • dict.get can be called with a fallback value as the second argument. 可以使用回退值作为第二个参数来调用dict.get I'm telling it to just return the current character if it cannot be found within the dictionary. 如果在字典中找不到它,我告诉它只返回当前字符。
  • I'm using a list comprehension as the argument for str.join instead of a generator expression for performance reasons, here's an excellent explanation . 由于性能原因,我使用列表str.join作为str.join的参数而不是生成器表达式,这是一个很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 中的列表/字典理解,使用字符串中的键和值更新字典 - List/Dict comprehension in Python to update a dictionary with keys and values from a string 根据嵌套字典列表中的匹配键创建值字典 - create dictionary of values based on matching keys in list from nested dictionary 根据字典的键和值创建字符串 - Creating a string from the keys and values of a dictionary 在字典和嵌套数组中查找匹配键并用值替换数组 - Find matching keys in dictionary and nested array & replace array with values from 如何根据匹配键将一个字典中的值添加到另一个字典中? - How to add values from one dictionary to another based on matching keys? 匹配字典中的键并创建具有关联值的列表 - matching keys from dictionary and create lists with associated values 将字符串的字符与字典的键匹配,如果匹配,则将字符串转换为键的值 - Matching characters of a string with keys of a dictionary and converting strings to values of keys if matched 基于来自另一个字典的整数值从字典列表值中随机采样,具有匹配的键 - Random sampling from dictionary list values based on an integar value from another dictionary, with matching keys 从文本到字典,更新键 - From text to dictionary, update keys 从字典打印键和值 - print keys and values from dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM