【问题标题】:Change the values of a list without using index在不使用索引的情况下更改列表的值
【发布时间】:2015-06-09 17:38:59
【问题描述】:
def apply_coder(text, coder):
    """
    Applies the coder to the text. Returns the encoded text.

    text: string
    coder: dict with mappings of characters to shifted characters
    returns: text after mapping coder chars to original text
    """

    coder_dict = coder
    text_list = list(text)
    for letter in text_list:
        if letter in coder_dict:
            letter = coder_dict[letter]
            print letter
    return ''.join(text_list)

示例: apply_coder("Hello, world!", build_encoder(3)) 应该返回

'Khoor,czruog!'

相反,它返回

“你好,世界!”

但是,如果我在 if 条件下使用 print letter 语句,它会打印出正确的字母。

【问题讨论】:

  • 您将print 语句(打印编码字母)与return 语句(返回原始输入)混合在一起。您需要根据编码字母构建一个新列表。
  • 你应该在课程 mitx 论坛上提问

标签: python list python-2.7 dictionary


【解决方案1】:

重新绑定当前迭代变量不会修改列表(希望 - 这将是一个非常烦人的副作用)。

另外,将coder 重新绑定到coder_dict 是没有用的,您可以迭代字符串。长话短说,解决方案是用修改后的字母重新构建一个列表......由于您并不真的需要列表本身,您也可以只使用生成器表达式:

def apply_coder(text, coder):
    return ''.join(coder.get(letter, letter) for letter in text)

【讨论】:

    【解决方案2】:

    通过更改letter,您不会更改列表text_list 中的任何内容。您应该构建一个新列表而不是编码结果:

    def apply_coder(text, coder):
        ret_val = []
        coder_dict = coder
        for letter in text:
            if letter in coder_dict:
                letter = coder_dict[letter]
                print letter
            ret_val.append(letter)
        return ''.join(ret_val)
    

    这也意味着您不必首先将text 放入列表中。

    【讨论】:

      【解决方案3】:

      语句letter = coder_dict[letter] 只是在coder_dict 中查找letter 键并将该值分配给letter,这不会改变它。它打印那封信。但是,您根本没有修改 text_list。有几种方法可以做到这一点,例如enumerate

      def apply_coder(text, coder):
          text_list = list(text)
          for val,letter in enumerate(text_list):
              text_list[val] = coder.get(letter, letter)
          return ''.join(text_list)
      

      这将枚举list,然后在可能的情况下使用字母查找或字母本身重新分配每个索引元素,然后join() listreturn 它。

      【讨论】:

        【解决方案4】:

        这与不久前提出的关于加密/交换字母的问题非常相似。

        以下代码由 alec_djinn 提供

        decrypted = 'abcdefghijklmnopqrstuvwxyz' #normal alphabet
        encrypted = 'MNBVCXZLKJHGFDSAPOIUYTREWQ' #your "crypted" alphabet
        
        #Encription
        text = 'cryptme' #the string to be crypted
        encrypted_text = ''
        for letter in text:
            encrypted_text += encrypted[decrypted.find(letter)]
        print encrypted_text
        #will print BOWAUFC
        
        #Decription
        text = encrypted_text #"BOWAUFC" in this example
        decrypted_text = ''
        for letter in text:
            decrypted_text += decrypted[encrypted.find(letter)]
        print decrypted_text
        #will print cryptme
        

        下面也提供了一个链接。

        How to 'encrypt' a file

        【讨论】:

          猜你喜欢
          • 2021-06-15
          • 1970-01-01
          • 2022-01-07
          • 2022-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-21
          • 2019-09-08
          相关资源
          最近更新 更多