创建一个将数字转换为字母的函数

我编写了这个函数,它应该通过用户提供的字符串(如 )1-3-5,并输出相应的一系列字母,其中 A 分配给 1,B 分配给 2,C 分配给 3 等。输出的情况1-3-5是ACE. 对于2-3-4,它应该打印BCD。对于?-3-4或者--3-4它仍然应该打印BCD.这是我到目前为止编写的代码:


def number_to_letter(encoded):

    result = ""

    start = 0

    for char in range(len(encoded)):

        if encoded[char] == '-':

            i = encoded.index("-")

            sub_str = encoded[start:i]

            if not sub_str.isdigit():

                result += ""

            else:

                letter = chr(64 + int(sub_str))

                if 0 < int(sub_str) < 27:

                    result += letter


                else:

                    result += ""


            start += len(sub_str) + 1

    return result


print(num_to_let('4-3-25'))

我的输出是D,当它应该是的时候DCY。我试图在不使用任何列表或使用该split函数的情况下执行此操作,只需查找-子字符串中的字符并将其前面的数字转换为字母即可。我能做些什么?


湖上湖
浏览 155回答 4
4回答

慕田峪4524236

你可以尝试做这样的事情:def number_to_letter(encoded):&nbsp; &nbsp; result&nbsp; = ""&nbsp; &nbsp; buffer = ""&nbsp; &nbsp; for ch in encoded:&nbsp; &nbsp; &nbsp; &nbsp; if ch == '-':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if buffer and 0 < int(buffer) < 27:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += chr(64 + int(buffer))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer = ""&nbsp; &nbsp; &nbsp; &nbsp; elif ch.isdigit():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer += ch&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; if buffer and 0 < int(buffer) < 27:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += chr(64 + int(buffer))&nbsp; &nbsp; &nbsp; &nbsp; return result&nbsp;print(number_to_letter('1-3-5'))输出:ACE解释:我们循环每个字符并将其添加到某个缓冲区中。当我们遇到-(分隔符)时,我们尝试解析缓冲区并重置它。最后我们再进行一次相同的解析并返回结果。验证的工作方式是,每当我们填充缓冲区时,我们都会检查数字有效性(使用.isdigit()),并且当我们解析缓冲区时,我们会检查范围约束。

元芳怎么了

没有清单,好吧。但是听写呢?def abc(nums):&nbsp; &nbsp; d = {'-':'','1':'A','2':'B','3':'C','4':'D','5':'E','6':'F','7':'G','8':'H','9':'I','0':'J'}&nbsp; &nbsp; res = ''&nbsp; &nbsp; for n in nums: res += d[n]&nbsp; &nbsp; return res&nbsp; &nbsp;&nbsp;print(abc('1-2-3-9-0')) # Output: ABCIJ这是一个更正的版本:def abc(nums):&nbsp; &nbsp; d = {'-':'','1':'A','2':'B','3':'C','4':'D','5':'E','6':'F','7':'G','8':'H','9':'I','0':'J'}&nbsp; &nbsp; res = ''&nbsp; &nbsp; for n in nums:&nbsp; &nbsp; &nbsp; &nbsp; if n in d:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res += d[n]&nbsp; &nbsp; return resprint(abc('?-2-3-9-0')) # Output: BCIJ

哔哔one

这段代码的方法是找到第一个“-”,然后将其存储在哪里,这样下次我们就可以在最后一个“-”之后查找第一个“-”当我的代码中的注释谈论循环时意味着要经历一次循环(循环时:)def number_to_letter(encoded):&nbsp; &nbsp; letterString = ""&nbsp; &nbsp; startSubStr = 0&nbsp; &nbsp; endSubStr = 0&nbsp; &nbsp; looping = True&nbsp; &nbsp; while looping:&nbsp; &nbsp; &nbsp; &nbsp; if endSubStr > (len(encoded)-4):# if we're at the last number we don't look for '-'. we go to the end of the str and end the loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endSubStr = len(encoded)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; looping = False&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endSubStr = encoded.index('-', startSubStr) #find the first '-' after the '-' found in the last cycle&nbsp; &nbsp; &nbsp; &nbsp; number = int(encoded[startSubStr:endSubStr]) #get the number between the '-' found in the last cycle through this loop and the '-' found in this one&nbsp; &nbsp; &nbsp; &nbsp; if number < 27:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; letter = chr(64 + int(number))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; letterString += letter&nbsp; &nbsp; &nbsp; &nbsp; startSubStr = endSubStr + 1 #set the start of the substring to the end so the index function doesn't find the '-' found in this cycle again&nbsp;&nbsp; &nbsp; return letterStringprint(number_to_letter("23-1-1-2")) #>>> WAAB结果:WAAB

慕雪6442864

import stringalphabet = list(string.ascii_lowercase)combination = "1-2-3"def seperate(s, sep='-'):&nbsp; &nbsp; return [s[:s.index(sep)]] + seperate(s[s.index(sep)+1:]) if sep in s else [s]combination = seperate(combination)print("".join([alphabet[int(i)-1] for i in combination]))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python