Ax**3+Bcos(x+y)+Cexp(-x**2)-bx+cy
怎么提取出。(系数与项之间有⭐,这里没显示出来)
A x3
B cos(x+y)
C exp(-x2)
-b x
c y
A x3
B cos(x+y)
C exp(-x2)
-b x
c y
一个表达式怎么提取出系数和选项。(语言-python)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 脚踏南山 2022-07-22 11:07关注
下有代码,可直接复制使用。如有帮助,敬请采纳,你的采纳是我前进的动力,O(∩_∩)O谢谢!!!!!!!!
路过的朋友也可以点个赞~(≧▽≦)/~
测试公式
A*x**3+B*cos((x+y)+(x1*x-y**y1))+C*exp(-x**2)-b*x+c*y
A*x**3+B*cos((x+y)+(x1*x-y**y1))+C*x*exp(-x**2)-b*x+c*y*x
A*x**3+B*cos((x+y)+(x1*x-y**y1))+C*x*exp(-x**2)-b*x+c*y*x+A/B*x+B/A*y
不可完成:系数相同的表达式,有这方面的要求再增加import re representation1 = "A*x**3+B*cos(x+y)+C*exp(-x**2)-b*x+c*y" representation1 = representation1.replace(' ','') if representation1[0] not in '-+': representation1 = '+' + representation1 representation2 = representation1.replace('**', '^') # 去掉最短括号,防干扰 # representation3 = re.sub('\(.*?\)', '', representation2) brackets_num_left = 0 brackets_num_right = 0 representation3 = representation1 replace = '' for x in representation1: if '(' in replace: replace += x if x == '(': if replace == '': replace = x brackets_num_left += 1 if x == ')': brackets_num_right += 1 if replace != '' and brackets_num_left == brackets_num_right: representation3 = representation3.replace(replace, '') replace = '' # 获取系数 系数 = re.findall('[+\-].*?[A-Za-z]', representation3) # 通过系数获取系数项 result = {} result_str = '' for x_i in range(len(系数)): try: res = re.findall(f'\{系数[x_i]}(.*?)\{系数[x_i + 1]}', representation1) result[系数[x_i]] = res[0] except: result[系数[x_i]] = representation1.split(系数[-1])[-1] result_str += 系数[x_i]+','+result[系数[x_i]]+',' print(result_str[:-1])
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 5无用
悬赏问题
- ¥60 ESP32怎么烧录自启动程序
- ¥50 html2canvas超出滚动条不显示
- ¥15 MATLAB四叉树处理长方形tif文件
- ¥15 java业务性能问题求解(sql,业务设计相关)
- ¥15 52810 尾椎c三个a 写蓝牙地址
- ¥15 elmos524.33 eeprom的读写问题
- ¥15 使用Java milo连接Kepserver服务端报错?
- ¥15 用ADS设计一款的射频功率放大器
- ¥15 怎么求交点连线的理论解?
- ¥20 软件开发方法学习来了