将移动数字键盘数字转换为相应的单词Python

2024-07-07 08:47:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个函数,如果我给出一个输入,比如999933。它的输出应为“ze”。它基本上起着数字手机键盘的作用。我怎么能这样呢。我在网上搜索了一些样品。我所得到的一切完全相反。比如,输入文本,你就会得到数字。我无法获得确切的流量,如何实现这一点。请让我知道,我该怎么做

def number_to_text(val):

pass

Tags: to函数text文本numberdef样品数字
3条回答

其他答案都是正确的,但我试着写一篇不那么简短、更真实的解释(包括博士测试),来解释之前的结果是如何工作的:

dialpad_text.py

# Import the groupby function from itertools, 
# this takes any sequence and returns an array of groups by some key
from itertools import groupby

# Use a dictionary as a lookup table
dailpad = {
  '2': ['a', 'b', 'c'],
  '3': ['d', 'e', 'f'],
  '4': ['g', 'h', 'i'],
  '5': ['j', 'k', 'l'],
  '6': ['m', 'n', 'o'],
  '7': ['p', 'q', 'r', 's'],
  '8': ['t', 'u', 'v'],
  '9': ['w', 'x', 'y', 'z'],
}

def dialpad_text(numbers):
  """
  Takes in either a number or a string of numbers and creates
  a string of characters just like a nokia without T9 support

  Default usage:
  >>> dialpad_text(2555222)
  'alc'

  Handle string inputs:
  >>> dialpad_text('2555222')
  'alc'

  Handle wrapped groups:
  >>> dialpad_text(2222555222)
  'alc'

  Throw an error if an invalid input is given
  >>> dialpad_text('1BROKEN')
  Traceback (most recent call last):
    ...
  ValueError: Unrecognized input "1"
  """

  # Convert to string if given a number
  if type(numbers) == int:
    numbers = str(numbers)

  # Create our string output for the dialed numbers
  output = ''

  # Group each set of numbers in the order 
  # they appear and iterate over the groups.
  # (eg. 222556 will result in [(2, [2, 2, 2]), (5, [5, 5]), (6, [6])])
  # We can use the second element of each tuple to find 
  # our index into the dictionary at the given number!
  for number, letters in groupby(numbers):
    # Convert the groupby group generator into a list and
    # get the offset into our array at the specified key
    offset = len(list(letters)) - 1

    # Check if the number is a valid dialpad key (eg. 1 for example isn't)
    if number in dailpad.keys():
      # Add the character to our output string and wrap
      # if the number is greater than the length of the character list
      output += dailpad[number][offset % len(dailpad[number])]
    else:
      raise ValueError(f'Unrecognized input "{number}"')

  return output

希望这有助于您了解较低级别的情况!另外,如果您不信任我的代码,只需将其保存到一个文件并运行python -m doctest dialpad_text.py,它将从模块中传递doctests。

(注意:如果没有-v标志,它将不会输出任何内容,沉默是金色的!)

你需要

  • 将相同的数字与捕获数字的regex (\d)\1*组合在一起,然后将相同的数字X倍
  • 使用组中数字的值获取密钥
  • 用它的长度来得到这封信
phone_letters = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]

def number_to_text(val):
    groups = [match.group() for match in re.finditer(r'(\d)\1*', val)]
    result = ""
    for group in groups:
        keynumber = int(group[0])
        count = len(group)
        result += phone_letters[keynumber][count - 1]
    return result


print(number_to_text("999933")) # ze

使用列表理解

def number_to_text(val):
    groups = [match.group() for match in re.finditer(r'(\d)\1*', val)]
    return "".join(phone_letters[int(group[0])][len(group) - 1] for group in groups)

创建一个将pad_编号映射到字母的映射。
使用itertools.groupby在连续的按键上迭代,并计算得到的字母

import itertools

letters_by_pad_number = {"3": "def", "9": "wxyz"}

def number_to_text(val):
    message = ""
    # change val to string, so we can iterate over digits
    digits = str(val)
    # group consecutive numbers: itertools.groupby("2244") -> ('2', '22'), ('4','44')
    for digit, group in itertools.groupby(digits):
        # get the pad letters, i.e. "def" for "3" pad
        letters = letters_by_pad_number[digit]
        # get how many consecutive times it was pressed
        presses_number = len(list(group))
        # calculate the index of the letter cycling through if we pressed 
        # more that 3 times
        letter_index = (presses_number - 1) % len(letters)
        message += letters[letter_index]
    return message


print(number_to_text(999933))
# ze

和硬核一行只是为了好玩:

letters = {"3": "def", "9": "wxyz"}
def number_to_text(val):
    return "".join([letters[d][(len(list(g)) - 1) % len(letters[d])] for d, g in itertools.groupby(str(val))])

print(number_to_text(999933))
# ze

相关问题 更多 >