Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-将10位数字的电话更改为具有列表的单词#简单_Python_List - Fatal编程技术网

Python-将10位数字的电话更改为具有列表的单词#简单

Python-将10位数字的电话更改为具有列表的单词#简单,python,list,Python,List,因此,我不太确定如何进行此操作。 我想将1替换为1,2替换为2,依此类推 我应该用一张清单来做这件事。。但我不知道该怎么做 用户输入一个10位数的号码,该号码被重新排列成一个电话号码,然后在适当的区域添加破折号 我已经完成了前两部分,但还不知道如何转换为 语言 编辑**我说这不是重复的,因为我一直在寻找一个简单的方法来做这件事。我看到的其他每一条线程都包含超出我理解水平的编码 def makePhoneNums(): original = getNumber() phone_nu

因此,我不太确定如何进行此操作。
我想将
1
替换为
1
2
替换为
2
,依此类推

我应该用一张清单来做这件事。。但我不知道该怎么做

用户输入一个10位数的号码,该号码被重新排列成一个电话号码,然后在适当的区域添加破折号

我已经完成了前两部分,但还不知道如何转换为 语言

编辑**我说这不是重复的,因为我一直在寻找一个简单的方法来做这件事。我看到的其他每一条线程都包含超出我理解水平的编码

def makePhoneNums():
    original = getNumber()
    phone_num = fixPhoneNum(original)
    phone_word = getWordForm(phone_num)
    printPhoneNums(original, phone_num, phone_word) 

def getNumber():
    original = input("Input a 10 digit number: ")
    while 10 != len(original) or original.isdecimal == False:
        original = input("Error! Input a 10 digit number!: ")
    print()
    return original

def fixPhoneNum(original):
    switched = original[-1] + original[5:9] + original[1:5] + original[0]
    phone_num = switched[:3] + '-' + switched[3:6] + '-' + switched[6:]
    return phone_num

def getWordForm(phone_num):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
             'eight', 'nine']
    s = '0123456789'
    telNum = ''
    for i in range(len(s)):
        telNum += words[int(s[i])]
    return words[i]

def printPhoneNums(original, phone_num, phone_word):
    print(original, '\t', phone_num, '\t    ', phone_word)

首先,你似乎返回了错误的东西

def getWordForm(phone_num):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
             'eight', 'nine']
    s = '0123456789'
    telNum = ''
    for i in range(len(s)):
        telNum += words[int(s[i])]
    return telNum  # <== return telNum here

我不确定您是否想在单词之间加空格,但这应该很容易理解。

代码中的一些问题-

  • 主要问题是,在您的
    getWordForm()
    函数中,您使用
    telNum
    创建字符串,但随后返回
    words[i]
    ,您应该返回
    telNum
    ,并且应该迭代函数的
    phone_num
    参数,而不是
    s
    。请注意,这不会在单词之间包含任何空格,更好的方法是使用
    '.join()
    ,如果需要空格。范例-

    def getWordForm(phone_num):
        words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
                 'eight', 'nine']
        return ' '.join([words[int(ch)] if ch.isnumeric() else ch for ch in phone_num])
    
    如果ch.isnumeric(),则需要条件
    ,因为您发送的是固定电话号码(中间带有
    -
    ),如果您不需要空格,只需将
    '
    (空字符串)用于
    str.join()

    如评论中所述,一种不使用
    str.join()的方法-

    请注意,没有
    str.join()
    的方法将不包括空格,我建议在此基础上使用
    str.join()

  • 第二个问题,您应该在
    getNumber()
    中调用函数
    isdecimal
    ,作为-
    而不是10!=len(原件)或original.isdecimal()==False:

  • 一艘班轮:

    call = lambda s: ' '.join(map(lambda c:['oh','one','two','three','four','five','six','seven','eight','niieeiieeine','-'][int(c) if c.isdigit() else -1],s))
    
    在一段美好的时光里:

    >>> print(call('8675309'))
    eight six seven five three oh niieeiieeine
    
    备选方案一班轮来自:

    IMO更漂亮,但不清楚是否更高效:

    In [75]: timeit.timeit("lambda s: ' '.join([['oh','one','two','three','four','five','six','seven','eight','niieeiiee‌​ine','-'][int(c) if c.isdigit() else -1] for c in s])('8675309')",number=200000000)
    Out[75]: 17.842306826962158
    
    In [76]: timeit.timeit("lambda s: ' '.join(map(lambda c:['oh','one','two','three','four','five','six','seven','eight','niieeiieeine','-'][int(c) if c.isdigit() else -1],s))('8675309')",number=200000000)
    Out[76]: 17.543266678927466
    
    或地图解决方案

    >>> '-'.join(map(lambda x: words[int(x)] if x.isdigit() else '', list(phone_number)))
    'one-two-three--one-two-three-four--one-two-three'
    
    在函数中

    def getWordForm(phone_number):
        words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
        return '-'.join([words[int(num)] for num in list(phone_number) if num.isdigit()])
    

    因此,您提供的代码确实有效。但是,我还没有使用.join、ch等。我需要遵循的规范包括保持我的理解水平。有没有更具介绍性的方法呢?你需要单词之间的空格吗?或者更好的办法是了解
    str.join()
    是如何工作的,这是一个简单而强大的工具。是的,我需要在单词和破折号之间留空格。只是我还没有在课堂上演示str.join(),如果我使用它,我会因为做实验而被扣分,而这个实验应该用我在课堂上已经教过的内容来完成。@CameronRogozinsky-为什么你会因为使用课堂上还没有教过的方法而被扣分?是不是因为这意味着你没有自己做作业?@TigerhawkT3这是我第一次接触计算机编程。我一直在做作业,但坦率地说,这个实验室是这学期最难的,我的实验室老师和教授都这么说。请告诉我,当你刚开始的时候,你有时并不感到不知所措。使用这种方法,我得到的结果是:“一”可爱(尽管你用Tommy Tutone ref:来显示你的年龄)。OTOH,
    lambda s:'.join([[oh','one','two','two','four','five','six','seven','eieieine','-'][int(c)if c.isdigit()else-1]表示s中的c])
    稍微短一些,避免了
    map
    函数调用。另外,Python3中的
    map
    返回一个迭代器,
    join
    需要将其转换为一个列表,因为
    join
    必须对其arg进行两次传递(第一次传递是计算目标字符串大小)。因此,传递
    join
    列表comp比传递迭代器或生成器稍微有效一些。@PM2Ring,很高兴有人得到了它。:)同意您的版本看起来更干净,但就时间而言,这两个版本在彼此的误差范围内。用计时更新了我的答案。很难从这样的东西中得到真实的计时数据。我怀疑构建数字单词列表的时间相对于列表comp循环超过7位所需的时间是不可忽略的。当然,您可以预先构建该列表,但这样它就不会是一个问题由于我前面提到的原因,在Python3和Python2上运行它可能会有速度上的差异。还是很好的讨论。
    In [75]: timeit.timeit("lambda s: ' '.join([['oh','one','two','three','four','five','six','seven','eight','niieeiiee‌​ine','-'][int(c) if c.isdigit() else -1] for c in s])('8675309')",number=200000000)
    Out[75]: 17.842306826962158
    
    In [76]: timeit.timeit("lambda s: ' '.join(map(lambda c:['oh','one','two','three','four','five','six','seven','eight','niieeiieeine','-'][int(c) if c.isdigit() else -1],s))('8675309')",number=200000000)
    Out[76]: 17.543266678927466
    
    >>> words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    >>> phone_number="123 1234 123"
    >>> '-'.join([words[int(num)] for num in list(phone_number) if num.isdigit()])
    'one-two-three-one-two-three-four-one-two-three'
    
    >>> '-'.join(map(lambda x: words[int(x)] if x.isdigit() else '', list(phone_number)))
    'one-two-three--one-two-three-four--one-two-three'
    
    def getWordForm(phone_number):
        words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
        return '-'.join([words[int(num)] for num in list(phone_number) if num.isdigit()])