将公式变量转换为变量名称,使用正则表达式操作。

huangapple go评论108阅读模式
英文:

Converting formula variables to variable names with regex operations

问题

以下是翻译好的部分:

我试图将变量 Formula_bit 转换为类似名称的变量,其中它们是小写,并且单词之间用下划线分隔。我的过程如下:将右侧按运算符(+、-、*、/)或 x(乘法)拆分,将结果转换为小写,将空格替换为下划线,删除开头和结尾的括号。最后,如果有任何下划线,将其删除。然而,我的 outputexpected outputs 不匹配,我应该怎么做才能解决这个问题?

import re 
Formula_bit = ['Σ (Dividends)', 'Dividend Payout Ratio * eps']

# 处理每个公式的右侧,以提取参数
params = [
    re.split(r'\s*[+\-*/]\s*| x ', re.sub(r'[+\-*/]', ',', item))[0]  # 通过运算符(+、-、*、/)或 'x'(乘法)拆分右侧
        .lower()  # 转换为小写
        .replace(" ", "_")  # 将空格替换为下划线
        .replace("(", "")  # 删除开括号
        .replace(")", "")  # 删除闭括号 
    for item in Formula_bit
]

# 从每个项中删除开头和结尾的下划线,并去除空白
params = [item.lstrip('_').rstrip('_').strip() for item in params]

输出:

['σ_dividends', 'dividend_payout_ratio_,_eps']

期望输出:

['σ_dividends', 'dividend_payout_ratio', 'eps']
英文:

I ma trying to convert the variable Formula_bit into variable like names where they are lowercase and words are seperated by _. My Process is as follows splitting the right-hand side by operators (+, -, *, /) or x (multiplication), converts the resulting items to lowercase, replaces spaces with underscores, removes opening and closing parentheses. Finally removing the leading and trailing underscores if there are any. However my output and expected outputs dont match what could I do to fix this?

import re 
Formula_bit = ['Σ (Dividends)', 'Dividend Payout Ratio * eps']

# Process the right-hand side of each formula to extract parameters
params = [
    re.split(r'\s*[+\-*/]\s*| x ', re.sub(r'[+\-*/]', ',', item))[0]  # Split the right-hand side by operators (+, -, *, /) or 'x' (multiplication)
        .lower()  # Convert to lowercase
        .replace(" ", "_")  # Replace spaces with underscores
        .replace("(", "")  # Remove opening parentheses
        .replace(")", "")  # Remove closing parentheses 
    for item in Formula_bit
]

# Remove leading and trailing underscores from each item and strip whitespace
params = [item.lstrip('_').rstrip('_').strip() for item in params]

Output:

['σ_dividends', 'dividend_payout_ratio_,_eps']

Expected output:

['σ_dividends', 'dividend_payout_ratio', 'eps']

答案1

得分: 1

import re
import string

Formula_bit = ['Σ (Dividends)', 'Dividend Payout Ratio * eps']  # 输入公式

splitter = "_"  # 用于替换空格的分隔符
formula = ",".join(Formula_bit)  # 将公式连接成一个字符串
formula = re.sub(r"[()]", "", formula.lower())  # 从公式字符串中删除括号
formula = re.sub(r"\s", splitter, formula)  # 将空格字符替换为分隔符
punctuation = string.punctuation.replace(splitter, "")  # 不包括分隔符的标点符号
formula = re.sub(fr"[{punctuation}]", ",", formula)  # 从公式字符串中删除标点字符
params = 
展开收缩
# 在逗号上分割公式字符串以提取参数并删除分隔符字符
print(params) # ['σ_dividends', 'dividend_payout_ratio', 'eps']

这里有一个遗漏的检查。要成为有效的变量名称,第一个字符应为字母(而不是数字)。

英文:

Example, that converts formula to variable names

import re
import string

Formula_bit = ['Σ (Dividends)', 'Dividend Payout Ratio * eps']  # Input formulas

splitter = "_"  # Splitter character for replacing spaces
formula = ",".join(Formula_bit)  # Join the formulas into a single string
formula = re.sub(r"[()]", "", formula.lower())  # Remove parentheses from the formula string
formula = re.sub(r"\s", splitter, formula)  # Replace whitespace characters with the splitter
punctuation = string.punctuation.replace(splitter, "")  # Punctuation excluding the splitter
formula = re.sub(fr"[{punctuation}]", ",", formula)  # Remove punctuation characters from the formula strin
params = 
展开收缩
# Split the formula string on commas to extract the parameters and strip splitter characters
print(params) # ['σ_dividends', 'dividend_payout_ratio', 'eps']

Here one check is missed. To be a valid variable name, first character should be a letter (not digit).

huangapple
  • 本文由 发表于 2023年6月1日 13:10:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定