0

Do you know how I can check the amount of symbols that an expression has, so I can only save the ones with 3 symbols or less? Like a + b + c is what I want to keep but a + b + c + d not.

I have no clue where to even start with solving this problem.

The problem illustrated below: The bottom table contains many expressions that are too large and of no interest which I want to filter out.

Linear combination of equations in the upper table presented in the bottom table: Linear combination of equations in the upper table presented in the bottom table

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • can you share the code so i can see what kind of variables they are? – anarchy Jul 12 '21 at 09:44
  • 1
    Please avoid posting images (or worse, links to images) of code or errors. Anything text-based (code and errors) should be posted as text directly in the question itself and formatted properly as a [mre]. You can get more [formatting help here](https://stackoverflow.com/help/formatting). You can also read about [why you shouldn't post images/links of code](//meta.stackoverflow.com/q/285551). – Tomerikoo Jul 12 '21 at 09:45

2 Answers2

2

This might be an overkill, but you can use python's own tokenizer to tokenize the expression and then filter on the tokens. https://docs.python.org/3/library/tokenize.html

Abby Yan
  • 21
  • 1
0

I just figured it out, all I needed has the .has() function and an array with my symbols, in this case it's sym2.

Here you can see the code I implemented.

plus = []
minus = []
#counters 
cp = 0
cm = 0
for eq1 in mlistes[0:, 1]:
    for eq2 in mlistes[0:, 3]:
        tempp = eq1 + eq2
        tempm = eq1 - eq2
        for i in range(0, 8):
            for check in sym2[i]:
                if tempp.has(check):
                    cp = cp + 1
                if tempm.has(check):
                    cm = cm + 1
        if (cp <= 3) and (tempp != 0): 
            plus.append(tempp)
        if (cm <= 3) and (tempp != 0):
            minus.append(tempm)
        cp = 0
        cm = 0