【问题标题】:Trouble finding easy way to translate string based on key找不到基于键翻译字符串的简单方法
【发布时间】:2017-06-26 18:50:23
【问题描述】:

基本上,我有一个可以非常简单地加密消息的函数。

def encrypt(message):
    alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    key = ["4","x","z","@","%","b","j","q","(","ƒ","¥","µ","˚","nå","ø","π","å","œ","¢","∞","∫","µ","≈","`","¬","…"]
    new_message = ""
    for x in range(0,len(message)):
        new_message = message.replace(message[x],key.index[alphabet.index(message[x])])
    return new_message

print(encrypt(input("What would you like to encrypt").lower()))

这应该取字母并将其替换为列表键中具有相同索引的字符,但是我收到错误:

TypeError: 'builtin_function_or_method' object is not subscriptable

【问题讨论】:

  • 有更好的方法来做到这一点,但你的问题是key.index[alphabet.index(message[x])]。请改用key[alphabet.index(message[x])]
  • @JaredGoguen 我认为这行不通,有些键包含在字母表中。这是str.translate 的用例。
  • @juanpa.arrivillaga 哈哈,同意...直到我发布答案后才看到这条消息。

标签: python list python-3.x indexing


【解决方案1】:

终于有了str.translate的用例!

def encrypt(message):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    key = '4xz@%bjq(ƒ¥µ˚nåøπ圢∞∫µ≈`¬'
    table = str.maketrans(alphabet, key)

    return message.translate(table)

print(encrypt('asdsaewqeq')) # 4œ@œ4%µπ%π

请注意,key 中的一个条目由两个字符组成。如果这是故意的,并且您想用两个替换单个字符,那么您可以手动创建翻译表。

table = dict(zip(map(ord, alphabet), key))

【讨论】:

  • @Sumtinlazy 你读过str.translate 链接的文档吗?
  • 没看到,我的错。
【解决方案2】:

key.index() 接受一个值并返回它的索引,它使用() 而不是[] 所以你需要修复这一行:

new_message = message.replace(message[x],key.index[alphabet.index(message[x])])

到:

new_message = message.replace(message[x],key[alphabet.index(message[x])])

这将获取字母索引并使用它访问key 列表并获取该索引处的值以将其替换为原始字母。

编辑: 一个更好的方法是使用dictionary 并构造一个新的字符串,以避免在string 上出现双重replace()

dic = {'a': '4', 'b': 'x', 'c': 'z' ...}
new_message = ''
for x in message:
    new_message += dic[x]
return new_message

【讨论】:

    【解决方案3】:

    我建议使用中间dict 来创建alphabet 列表项与key 列表项的映射:

    >>> alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    >>> key = ["4","x","z","@","%","b","j","q","(","ƒ","¥","µ","˚","nå","ø","π","å","œ","¢","∞","∫","µ","≈","`","¬","…"]
    
    # Your `dict` object with the mapping between both the list
    >>> encryption_dict = dict(zip(alphabet, key))
    

    然后使用上面的字典和str.join(...) 来转换你的字符串。例如:

    >>> my_str = 'stackoverflow'
    
    #  Transform the string using the `dict` and join the chars to form single string           
    >>> new_str = ''.join(encryption_dict.get(s, s) for s in my_str)
    #                                            ^
    #        to return same character if not present in alphabet list
    
    >>> print(new_str)
    ¢∞4z¥øµ%œbµø≈
    

    【讨论】:

    • 如果不使用中间虚拟值,这将不起作用,因为某些键是字母表。
    • @juanpa.arrivillaga 我认为您的意思是alphabet 列表中 中存在某些字符?我通过使用dict.get(..) 更新了处理它的答案
    • 这很好用,但是我想知道这与我之前尝试的有什么不同
    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 2020-06-18
    • 2023-03-31
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多