我需要从字符串中获取第一个字母,并使其成为python中其他字符串中另一个字母的索引

时间:2017-08-01 19:48:03

标签: python string dictionary indexing

我是编程的新手,所以我开始讲一个关于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')

1 个答案:

答案 0 :(得分:0)

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

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