【问题标题】:changing a word in a string?更改字符串中的单词?
【发布时间】:2012-10-03 23:32:50
【问题描述】:

我有这个任务:

编写一个函数smallnr(x),它接受一个数字x,如果x是一个 06 之间的整数,它返回数字的名称, 否则它只是将x 作为字符串返回。

我做了以下事情:

def smallnr(x):
    if x>6:
        return str(x)
    else:
        lst=['zero','one','two','three','four','five','six']
        return (lst[x])

这行得通,但现在我必须这样做:

使用a部分中的函数smallnr,编写一个函数 convertsmall(s) 将文本 s 作为输入并返回文本 s 将小数(0 到 6 之间的整数)转换为它们的 名字。例如,

convertsmall('我有 5 个兄弟和 2 个姐妹,共有 7 个兄弟姐妹。') “我有五个兄弟和两个姐妹,一共有 7 个兄弟姐妹。”

我知道我需要以某种方式使用split()isnumeric(),但我不知道如何将它们放在一起并仅更改字符串中的数字。

有什么建议吗?

【问题讨论】:

  • 我会使用正则表达式搜索\b(\d+)\b

标签: python split isnumeric


【解决方案1】:

这是最简单的方法(在我看来):

def convertsmall(text):
    return ' '.join(smallnr(int(word)) if word.isdigit() else word for word in text.split())

输出:

>>> convertsmall('I have 5 brothers and 2 sisters, 7 siblings altogether.')
'I have five brothers and two sisters, 7 siblings altogether.'

要理解这一点,让我们倒退:

  1. 使用text.split() 将字符串划分为单词list - 如果没有传递参数,split() 使用' '(空格)作为分隔符来划分字符串。
  2. smallnr(int(word)) if word.isdigit() else word - 如果word 是一个数字,则调用smallnr(),否则返回word 不变。
  3. 由于word 是一个字符串,我们需要使用int(word) 将其转换为整数,然后再将其传递给您的函数,该函数假定x 是一个整数。
  4. 整个短语是一个列表解析,它处理text.split() 中的每个word 以产生一个新列表。我们使用 ' '.join(list) 将列表中的 words 连接在一起,用空格分隔。

希望说明清楚:)

【讨论】:

  • 这不是最好的表达方式......但它确实完成了工作!所以我希望教授能解决这个问题……谢谢!
  • 好吧,我给你说得更好了:)
【解决方案2】:
  1. 拆分句子(在空格上)
  2. 遍历单词(来自拆分)
  3. 如果单词 isnumeric 将其替换为函数的结果
  4. 让他们重新聚在一起
  5. 返回结果

【讨论】:

    【解决方案3】:

    所以你想把你传递给 convertsmall 函数的句子字符串用空格分割。您可以通过获取您的字符串并调用.split(' ')(例如'hello world'.split(' ')mystring.split(' '))来做到这一点。这将为您提供一个拆分数组,例如 ['hello', 'world']

    然后您需要遍历生成的数组并查找数字或整数,然后将它们传递给您的函数并获取字符串值并将数组中的值替换为字符串值。

    您需要在浏览完每个单词并转换数字后加入最终的数组。你可以这样做' '.join(myArray)

    【讨论】:

      【解决方案4】:

      基于regex而不是split()的解决方案:

      def convertsmall(s):
          out = ''
          lastindex=0
          for match in re.finditer("\\b(\\d+)\\b", s):
              out += s[lastindex:match.start()]
              out += smallnr(int(match.group()))
              lastindex = match.end()
          return out + s[lastindex:]
      

      【讨论】:

        【解决方案5】:
        d={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six'}
        parts = my_string.split() #split into words
        new_parts = [d[p] if p in d else p for p in parts] #list comprehension to replace if possible
        print " ".join(parts) #rejoin 
        

        我认为会起作用

        >>> mystring = 'I have 5 brothers and 2 sisters, 7 siblings altogether.'
        >>> parts = mystring.split() #split into words
        >>> d={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six'}
        >>> new_parts = [d[p] if p in d else p for p in parts] #list comprehension to replace if possible
        >>> print " ".join(new_parts) #rejoin
        I have five brothers and two sisters, 7 siblings altogether.
        

        【讨论】:

        • 第二个代码块的第一行应该是>>> mystring = 'I have 5 brothers...,你的应该是five
        猜你喜欢
        • 1970-01-01
        • 2012-10-15
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2014-11-05
        相关资源
        最近更新 更多