使用列表理解从文本中过滤掉数字
Filter out numbers from text using list comprehension
我有一个带有货币值的小文本。我只想在列表中保存没有货币符号的值。到目前为止,这是我的工作示例
Text = "Out of an estimated million budget, million is channeled to program XX, million to program YY and the remainder to program ZZ"
amount = [x for x in Text.split() if x.startswith('$')]
当前代码保存值(带货币符号)。我如何去掉美元符号的值?
使用正则表达式:
import re
re.findall(r"$(\d+)", s)
试试这个
Text = "Out of an estimated million budget, million is channeled to program XX, million to program YY and the remainder to program ZZ"
# slice $ sign off of each string
amount = [x[1:] for x in Text.split() if x.startswith('$')]
amount
['5', '3', '1']
使用它从列表中删除第一个字符
amount = [x[1:] for x in Text.split() if x.startswith('$')]
amount = [x.replace('$','') for x in Text.split() if x.startswith('$')]
我有一个带有货币值的小文本。我只想在列表中保存没有货币符号的值。到目前为止,这是我的工作示例
Text = "Out of an estimated million budget, million is channeled to program XX, million to program YY and the remainder to program ZZ"
amount = [x for x in Text.split() if x.startswith('$')]
当前代码保存值(带货币符号)。我如何去掉美元符号的值?
使用正则表达式:
import re
re.findall(r"$(\d+)", s)
试试这个
Text = "Out of an estimated million budget, million is channeled to program XX, million to program YY and the remainder to program ZZ"
# slice $ sign off of each string
amount = [x[1:] for x in Text.split() if x.startswith('$')]
amount
['5', '3', '1']
使用它从列表中删除第一个字符
amount = [x[1:] for x in Text.split() if x.startswith('$')]
amount = [x.replace('$','') for x in Text.split() if x.startswith('$')]