![](/img/trans.png)
[英]Python - Need to extract the first and last letter from a word string
[英]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.