【问题标题】:I need to get the first letter from a string and make it an index to an other letter from other string in python我需要从一个字符串中获取第一个字母,并使其成为python中其他字符串中另一个字母的索引
【发布时间】:2018-01-08 19:58:57
【问题描述】:

我是编程新手,所以我从一门关于 python 的课程开始,在这门课程中我发现了这个我不知道该怎么做的练习,所以如果有人可以帮助我

>def cipher(map_from, map_to, code):
   """ map_from, map_to: strings where each contain 
                          N unique lowercase letters. 
      code: string (assume it only contains letters also in map_from)
        Returns a tuple of (key_code, decoded).
        key_code is a dictionary with N keys mapping str to str where 
        each key is a letter in map_from at index i and the corresponding 
        value is the letter in map_to at index i. 
        decoded is a string that contains the decoded version 
        of code using the key_code mapping. """

例如,

cipher("abcd", "dcba", "dab") 返回(字典中的条目顺序可能不一样)({'a':'d', 'b': 'c', 'd': 'a', 'c': 'b'}, 'adc')

【问题讨论】:

    标签: python string dictionary indexing


    【解决方案1】:

    字符的索引从 0 开始。这应该提供一个提示:

    >>> 'abcdefgh'.find('e')
    4
    >>> 'abcdefgh'[4]
    'e'
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      相关资源
      最近更新 更多