Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 从筛选器中排除数字_Python 3.x_Filter_Output - Fatal编程技术网

Python 3.x 从筛选器中排除数字

Python 3.x 从筛选器中排除数字,python-3.x,filter,output,Python 3.x,Filter,Output,我正在从字符串中筛选一组数字。我要做的代码是: def fun(variable): numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '$'] if variable in numbers: return True else: return False sequence = stats filtered = filter(fun, sequence) print(

我正在从字符串中筛选一组数字。我要做的代码是:

def fun(variable):
    numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '$']
    if variable in numbers:
        return True
    else:
        return False


sequence = stats
filtered = filter(fun, sequence)
print('The filtered numbers are:')
for s in filtered:
    print(s)
变量“stats”是一个字符串,如“24小时低点:$13.45” 当代码运行时,它将提供2、4、1、3、4、5的输出 然而,我想排除像24这样的数字,这些数字对于纯数据来说并不重要。 我要做什么改变才能得到这个?
另外,我对python和一般的编码都是新手,请原谅我的无知。

我会以不同的方式处理这个问题

假设所有数据字符串都遵循“:$”模式,则可以对字符串使用
.split
方法。如果拆分“$”上的字符串,则返回列表中的最后一项是可以转换为浮点的字符串

def get_price(string):
    """Return the last token from splitting a string on the '$' as a float."""
    return float(string.split("$")[-1])

data = ["24hour low: $13.45", "24hour high: $23.45"]

print(list(map(get_price, data)))

# A list comprehension that is the equivalent of the above.
print([get_price(item) for item in data])
输出:
[13.45,23.45]

更新:我刚看到你的评论。如果你想要一串数字,你可以使用这个代码

def get_price(string):
    """Return the last token from splitting a string on the '$'
    followed by a split on the '.' 
    followed by a join with an empty string ''."""
    return "".join(string.split("$")[-1].split("."))

data = ["24hour low: $13.45", "24hour high: $23.45"]

print(list(map(get_price, data)))

# A list comprehension which is the equivalent of the above.
print([get_price(item) for item in data])
输出:
['1345','2345']

如果您坚持使用
过滤器
,下面是一个检查每个字符是否为数字的示例。然后将项目索引2切片并连接到末尾。这将删除“24”

def is_digit(string):
    return string.isdigit()

stats = "24hour low: $13.45"
print(''.join(filter(is_digit, stats))[2:])
输出:“1345”

更新:我假设您有一个字符串列表。这里有一个例子

data = ["24hour low: $13.45", "24hour high: $23.45"]

print([''.join(filter(is_digit, string))[2:] for string in data])
输出:
['1345','2345']

包含示例字符串的注释后更新:

SAMPLE_STRING = "Key metrics24 Hour Low$21.2024 Hour High$22.90Net change$0.616816"

nested_tokens = [
    [''.join(filter(is_digit, item)) for item in token.split()]
    for token in SAMPLE_STRING.replace(
        "Net change", " " # Replace 'Net change' with a space
    ).split(
        "24 Hour " # Remove '24 Hour' to avoid ambiguity.
    )[
        1: # slice 'Key metrics' from split
    ]
]

import itertools as it

print(list(it.chain.from_iterable(nested_tokens)))

输出:
['2120'、'2290'、'0616816']

你能给出一个你期望/想要的输出的例子吗?@meshi而不是“24小时低:$13.45”我的目标是只得到1345。非常感谢,这回答了我的问题。然而,我想要过滤的数据是在变量下而不是字符串形式,我无法区分24小时低点和24小时高点。这还可能吗?很抱歉,我的问题措词有误。请提供您要筛选的数据样本,即包括您要使用的变量的值。我的变量是“序列=统计”示例中的“统计数据”:“关键指标24小时低点$21.2024小时高点$22.90净变动$0.616816”我在前面的评论中包含了一个使用示例字符串的解决方案。是否要将数据收集为类型
float
?没有小数点的字符串可能没有那么有用。这看起来很有希望,但我收到一个错误,说“is_digit”是一个未解析的引用。此外,我还发现了一系列回溯错误: