【问题标题】:trying to iterate over a string and translate message尝试遍历字符串并翻译消息
【发布时间】:2014-04-27 21:10:45
【问题描述】:

这是一个简单的练习,我认为我说得对,但可能存在一些小的语法误解。任何人都有更好的方法来做到这一点。只想遍历字符串并在必要时更改每个值,然后重新打印字符串

    alphabet = "abcdefghijklmnopqrstuvwxyz"
message = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."


key = list(alphabet)
message = list(message)


for index, i in enumerate(message):
    if i in key:
        if key.index(i)  == 25:
            message[index] = "b"

        elif key.index(i)  == 24:
            message[index] = "a"

        elif key.index(i) < 24:
            set_value = key[key.index(i) + 2]
            message[index] = set_value

for i in message:
    print i,

我已编辑代码以提供问题/谜题的最终答案。

输出:

i   h o p e   y o u   d i d n t   t r a n s l a t e   i t   b y   h a n d .   t h a t s   w h a t   c o m p u t e r s   a r e   f o r .   d o i n g   i t   i n   b y   h a n d   i s   i n e f f i c i e n t   a n d   t h a t ' s   w h y   t h i s   t e x t   i s   s o   l o n g .   u s i n g   s t r i n g . m a k e t r a n s ( )   i s   r e c o m m e n d e d .   n o w   a p p l y   o n   t h e   u r l .

【问题讨论】:

  • 你现在的问题到底是什么?为什么您认为可能存在“一些小的语法误解”?
  • @jonrsharpe:当我打印出最后的字母时,它只打印 [.]
  • @vikingcode 然后请将该信息添加到您的问题中
  • @SharkofMirkwood 这不是有效的 Python 语法

标签: python arrays string for-loop


【解决方案1】:

这段代码:

letters = [] # create empty list

for char in message:
    letters = [char] # replace letters with new list, containing only char

letters 保留为具有单个元素的列表; message 中的最后一个 char。如果您希望将整个 message 作为一个列表,那么最小的解决方法是:

for char in message:
    letters.append(char) # adds to the end of the list, rather than replacing it

或者你可以简单地这样做:

letters = list(message)

【讨论】:

    【解决方案2】:

    您可以迭代字符串并将新字符添加到“结果字符串”(进行适当的更改):

    result = ""
    
    for c in message:
        if c == "k":
            result += "m"
        elif c == "o":
            result += "q"
        elif c == "e":
            result += "g"
        else:
            result += c  # don't need changes, concatenate the same character
    
    print result
    

    【讨论】:

    • 没错,但我预计需要对字符串执行更复杂的操作,因此我决定将其放入列表中。大声笑,是的,每个字母都需要在字母表中移动两次才能正确解决:)
    • @vikingcode 在这种情况下,我建议建立一个字典 {old_char: new_char, ...} 而不是无数个 elif 分支。
    【解决方案3】:

    或者只是使用替换:

    print(message.replace('k','m').replace('o','q').replace('e','g'))
    

    【讨论】:

      【解决方案4】:

      谜语的答案为您提供了一个更紧凑实现的提示 - 使用 maketrans

      message = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
      
      rot2 = str.maketrans('abcdefghijklmnopqrstuvwxyz',
                           'cdefghijklmnopqrstuvwxyzab')
      print(message.translate(rot2))
      

      这适用于 python 3 - python 2 留给读者作为练习:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 2021-12-05
        • 1970-01-01
        • 2018-05-15
        相关资源
        最近更新 更多