Code: Alles auswählen
In [1]: import re
In [2]: equation = '-3.9x^2+5.2x-13.8'
In [3]: re.findall(r'(?<!\^)[+-]?\d+(?:\.\d+)?(?![*xX])', equation)
Out[3]: ['-3', '+5', '-13.8']
Code: Alles auswählen
In [1]: import re
In [2]: equation = '-3.9x^2+5.2x-13.8'
In [3]: re.findall(r'(?<!\^)[+-]?\d+(?:\.\d+)?(?![*xX])', equation)
Out[3]: ['-3', '+5', '-13.8']
Code: Alles auswählen
[x for x in re.split('-+', '-3.9x^2+5.2x-13.8') if 'x' not in x]
Code: Alles auswählen
>>> pattern = r'''
... (?# don't start with a caret)
... (?<!\^)
... (?# either a plus or a minus sign (optional))
... [+-]?
... (?# the number, can be a float)
... \d+(?:\.\d+)?
... (?# don't end with an asterisk, x or X)
... (?![*xX])
... '''
Code: Alles auswählen
[y+x for x,y in zip(re.split('-+', equation), re.findall('[-\+]', equation)) if 'x' not in x]
Code: Alles auswählen
[y+x for x,y in zip(re.split('-+', equation), re.findall('[-\+]', equation)) if 'x' not in x and x != '']
Code: Alles auswählen
In [29]: equation
Out[29]: '-3.9x^2-3.4+5.2x-13.8+5x^4-12.7'
In [30]: [y+x for x,y in zip(re.split('-+', equation), re.findall('[-\+]', equation)) if 'x' not in x and x != '']
Out[30]: ['+12.7']
In [31]: [y+x for x,y in zip(re.split('-+', equation), re.findall('[-\+]', equation)) if 'x' not in x]
Out[31]: ['-', '+12.7']
Code: Alles auswählen
cm-laptop:html cm$ isympy
Python 2.5.2 console for SymPy 0.6.3
These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z = symbols('xyz')
>>> k, m, n = symbols('kmn', integer=True)
>>> f, g, h = map(Function, 'fgh')
Documentation can be found at http://sympy.org/
In [1]: f = (-3.9 * x**2 + 5.2 * x - 13.8)
In [2]: f.args[0]
Out[2]: -13.8000000000000
In [10]: diff(f, x)
Out[10]: 5.20000000000000 - 7.80000000000000 * x
In [11]: integrate(f, x)
Out[11]:
2 3
-13.8000000000000 * x + 2.60000000000000 * x - 1.30000000000000 * x
Code: Alles auswählen
[y+x for x,y in zip(re.split('[-\+]', equation)[1:], re.findall('[-\+]', equation)) if 'x' not in x]
Code: Alles auswählen
if str[0] != '-' and str[0] != '+': str = '+' + str
Code: Alles auswählen
import re
achsenabschnitt = []
equation = '-3.9x^2-3.4+5.2x-13.8+5x^4-12.7'
if equation[0] != '-' and equation[0] != '+':
equation = '+' + equation
for x, y in zip(re.split('[-\+]', equation)[1:], re.findall('[-\+]', equation)):
if 'x' not in x:
achsenabschnitt.append(y+x)
Code: Alles auswählen
In [105]: from itertools import izip
In [106]: y_achsenabschnitte = []
In [107]: def foo(equation):
if not equation.startswith(('-', '+')):
equation = '+' + equation
for x, y in izip(re.split('[-+]', equation)[1:], re.findall('[-+]', equation)):
if 'x' not in x:
y_achsenabschnitte.append(y+x)
.....:
.....:
In [113]: foo(equation)
In [114]: y_achsenabschnitte
Out[114]: ['+2', '-3.4', '-13.8', '-12.7']
Und das weißt Du woher? Ich sehe in dem Code-Stück keine Gleichung, aus der das hervorginge!CM hat geschrieben: das mag zwar die Lösung für diese Aufgabe sein, aber den mathematischen Sinn kann ich nicht nachvollziehen, weil der Achsenabschnitt ein anderer ist, als die Summe der Zahlen in dieser Liste.
Nicht?CM hat geschrieben:das mag zwar die Lösung für diese Aufgabe sein, aber den mathematischen Sinn kann ich nicht nachvollziehen, weil der Achsenabschnitt ein anderer ist, als die Summe der Zahlen in dieser Liste.