我需要帮助将移动数字序列转换为实际句子
I need help converting mobile numeric sequence into actual sentences
我需要在 python 中创建一个可用的移动数字小键盘转换器。例如,当我输入“999 33 7777”时,它会打印“yes”。然后“0”输入将创建“”space 输出,就像旧移动键盘应该如何工作一样,输入“”space 将被忽略,就像我提到的“999” 33 7777" 将打印出 "yes" 而不是 "y e s"。但问题是当我在其他数字之间输入 space " " 以分隔单个整数和 double/triple 整数时,例如输入函数中的 "2"、"22" 和 "222"将打印出 nothing/blank。
请告诉我如何使用最少或没有外部模块来实现它。
translate = ["a", "b", "c","d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
translate_alpha = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999', '0']
def translator(val):
x=enumerate(translate_alpha)
output = ""
for i, j in x:
if j == val:
output+=output+translate[i]
return output
print(translator(input("")))
您可以首先创建一个将数字映射到字母的字典,方法是将两个列表压缩在一起,然后使用元组迭代器构造函数:
translate_dict = dict(zip(translate_alpha, translate))
然后,您可以简单地拆分空白,在字典中查找项目,然后连接空字符串:
def translator(val):
return "".join(translate_dict[x] for x in val.split())
我需要在 python 中创建一个可用的移动数字小键盘转换器。例如,当我输入“999 33 7777”时,它会打印“yes”。然后“0”输入将创建“”space 输出,就像旧移动键盘应该如何工作一样,输入“”space 将被忽略,就像我提到的“999” 33 7777" 将打印出 "yes" 而不是 "y e s"。但问题是当我在其他数字之间输入 space " " 以分隔单个整数和 double/triple 整数时,例如输入函数中的 "2"、"22" 和 "222"将打印出 nothing/blank。 请告诉我如何使用最少或没有外部模块来实现它。
translate = ["a", "b", "c","d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
translate_alpha = ['2', '22', '222', '3', '33', '333', '4', '44', '444', '5', '55', '555', '6', '66', '666', '7', '77', '777', '7777', '8', '88', '888', '9', '99', '999', '9999', '0']
def translator(val):
x=enumerate(translate_alpha)
output = ""
for i, j in x:
if j == val:
output+=output+translate[i]
return output
print(translator(input("")))
您可以首先创建一个将数字映射到字母的字典,方法是将两个列表压缩在一起,然后使用元组迭代器构造函数:
translate_dict = dict(zip(translate_alpha, translate))
然后,您可以简单地拆分空白,在字典中查找项目,然后连接空字符串:
def translator(val):
return "".join(translate_dict[x] for x in val.split())