gpt4 book ai didi

Python:两位数的 re.compile() 函数错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:11 29 4
gpt4 key购买 nike

此正则表达式需要找到 "1x""x1" 但它还必须能够找到两位数,例如 "10x"“x11”

leverage_match = re.compile(r"\d+X|X\d+", flags=re.IGNORECASE)

根据 regex101.com上面的正则表达式应该足以捕获以下的全部数字:

import pandas as pd
import re

df = pd.DataFrame(["BULL ESTOX 11X S", "BULL ESTOX X12 S"], columns=["name"])

name
"BULL ESTOX 11X S"
"BULL ESTOX X12 S"

但是,对于下面的代码,它只返回一个数字,例如对于 "11X",它变为 "1X"

leverage_match = re.compile(r"\d+X|X\d+", flags=re.IGNORECASE) #<- Same as seen above

def f(value):

f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else ""

leverage = f2(value)

if leverage != "":
return "{}".format(leverage)

if leverage[0].replace("X","x") == "x":
leverage = leverage[1]+leverage[0].replace('X','x')

df["description"] = df["name"].map(lambda x:f(x))

------------

更新:这是完整的代码,以确保我没有遗漏任何重要内容:

import pandas as pd
import re

df = pd.DataFrame(["BULL ESTOX 11X S", "BULL ESTOX X12 S"], columns=["name"])

description_map = {"ESTOX":"Euro STOXX 50"}
underlying_match = re.compile(r"\s(\S+)\s")
leverage_match = re.compile(r"\d+X|X\d+", flags=re.IGNORECASE)

def f(value):

f1 = lambda x: description_map[underlying_match.findall(x)[0]] if underlying_match.findall(x)[0] in description_map else ""
f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else ""
f3 = lambda x: "-" if "BEAR" in x else "-" if "SHORT" in x else ""

underlying = f1(value)
leverage = f2(value)
sign = f3(value)

statement = "Tracks " + underlying

if underlying == "":
if sign == "-" and leverage == "":
return statement + "{}".format("inversely.")
if sign == "-" and leverage != "":
return statement + "{} with {}{} leverage.".format("inversely", sign, leverage)
if sign == "" and leverage != "":
return statement + "with {}{} leverage.".format(sign, leverage)
else:
return "Tracks"

if leverage[0].replace("X","x") == "x":
leverage = leverage[1]+leverage[0].replace('X','x')

if leverage != "" and sign == "-":
statement += " {} with {}{} leverage.".format("inversely", sign, leverage)
elif leverage != "" and sign == "":
statement += " with {} leverage.".format(leverage)
else:
if sign == "-":
statement += " {} ".format("inversely")

return statement

df["description"] = df["name"].map(lambda x:f(x))

print df

最佳答案

我认为你给出了一个错误的例子,对于下面的 df

df = pd.DataFrame(["BULL AXP 11X S", "BULL AXP X11 S"], columns=["name"])

输出结果如下

             name                                 description
0 BULL AXP 11X S Tracks American Express with 11X leverage.
1 BULL AXP X11 S Tracks American Express with 1x leverage.

并且 x11 变成了 1x 因为我们的代码在以下部分的逻辑有错误:

if leverage[0].replace("X","x") == "x":
leverage = leverage[1]+leverage[0].replace('X','x')

它必须改为如下所示:(更新)

if leverage[0].replace("X","x") == "x":
leverage = ''.join(leverage[1:])+leverage[0].replace('X','x')

如果您修复了该问题,输出将如您所料,如下所示:

             name                                 description
0 BULL AXP 11X S Tracks American Express with 11X leverage.
1 BULL AXP X11 S Tracks American Express with 11x leverage.

完整代码

import pandas as pd
import re

df = pd.DataFrame(["BULL ESTOX 11X S", "BULL ESTOX X12 S"], columns=["name"])

description_map = {"ESTOX":"Euro STOXX 50"}
underlying_match = re.compile(r"\s(\S+)\s")
leverage_match = re.compile(r"\d+X|X\d+", flags=re.IGNORECASE)

def f(value):

f1 = lambda x: description_map[underlying_match.findall(x)[0]] if underlying_match.findall(x)[0] in description_map else ""
f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else ""
f3 = lambda x: "-" if "BEAR" in x else "-" if "SHORT" in x else ""

underlying = f1(value)
leverage = f2(value)
sign = f3(value)

statement = "Tracks " + underlying

if underlying == "":
if sign == "-" and leverage == "":
return statement + "{}".format("inversely.")
if sign == "-" and leverage != "":
return statement + "{} with {}{} leverage.".format("inversely", sign, leverage)
if sign == "" and leverage != "":
return statement + "with {}{} leverage.".format(sign, leverage)
else:
return "Tracks"

if leverage[0].replace("X","x") == "x":
leverage = ''.join(leverage[1:])+leverage[0].replace('X','x')

if leverage != "" and sign == "-":
statement += " {} with {}{} leverage.".format("inversely", sign, leverage)
elif leverage != "" and sign == "":
statement += " with {} leverage.".format(leverage)
else:
if sign == "-":
statement += " {} ".format("inversely")

return statement

df["description"] = df["name"].map(lambda x:f(x))

print df

关于Python:两位数的 re.compile() 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31070854/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com