簡體   English   中英

我需要從字符串中獲取第一個字母,並使其成為python中其他字符串的另一個字母的索引

[英]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的課程開始,在這門課程中,我發現該練習不知道怎么做,所以如果有人可以幫助我

>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')

字符從0開始索引。這應該提供提示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM