如何从字符串中提取以特定字母/字符开头的子字符串?

时间:2016-11-26 20:44:55

标签: python

例如,如果我有以下字符串:

fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."

我想提取一份价格清单:

['$3.00', '$2.00', '$10000']

到目前为止,我已经这样做了:

def extract_prices(s):
    prices = []
    for i in range(len(s)):
      if s[i] == '$':
        prices.append(s[i], s.find(' '))

我觉得最后一行给了我一些问题。我不知道如何获得价格后面的空间索引,以及如何在那里停止它。

任何提示?感谢您阅读本文!对不起,如果我的解释不清楚。

4 个答案:

答案 0 :(得分:4)

您可以使用正则表达式:

>>> fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."
>>> re.findall(r'(\$[\d.]+)', fruits)
['$3.00', '$2.00', '$10000.']

或者,如果您想要更具体,只要包含.,如果有数字:

>>> re.findall(r'(\$\d+(?:\.\d+)?)', fruits)
['$3.00', '$2.00', '$10000']

答案 1 :(得分:3)

拆分字符串并查找美元符号:

>>> fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."
>>> result = [item.strip(',.!?') for item in fruits.split() if '$' in item]
>>> result
['$3.00', '$2.00', '$10000']

请记住从每个项目中删除标点符号。

答案 2 :(得分:0)

使用以下正则表达式:

re.findall('\$\d+\.?\d+', fruits)

<强>输出:

>>> re.findall('\$\d+\.?\d+', fruits)
['$3.00', '$2.00', '$10000']

答案 3 :(得分:0)

如果您只想调整原始代码,请使用

  if s[i] == '$':
    lastPos = s.find(',', i)
    if lastPos == -1:
        lastPos = len(s)
    prices.append(s[i:lastPos])

而不是你的行

  if s[i] == '$':
    prices.append(s[i], s.find(' '))