I'm trying to access a list of string variables in a matrix to compare to a user inputted string in an if/elif statement. I can't seem to get the access right as my code prints the 'else' statement and doesn't do what I want...
from sympy.interactive import printing
printing.init_printing(use_latex = True)
from sympy import Eq, solve_linear_system, Matrix
import sympy as sp
NumberString = "four"
EvenMatrix = Matrix(["two" ,"four" ,"six","eight" ,"ten"])
display(EvenMatrix)
OddMatrix = Matrix(["one", "three", "five", "seven", "nine"])
display(OddMatrix)
if (NumberString == EvenMatrix):
display(NumberString)
print('The NumberString is even.')
elif (NumberString == OddMatrix):
display(NumberString)
print('The NumberString is odd.')
else:
print('wtf is going on?')
I'd like to compare the user input string to a list of strings in a matrices for cleaner code as opposed to something like this:
if (NumberString == EvenMatrix[0]) or (NumberString == EvenMatrix[1]) or (NumberString == EvenMatrix[2]) or (NumberString == EvenMatrix[3]) or (NumberString == EvenMatrix[4]):
display(NumberString)
print('The NumberString is even.')
elif (NumberString == OddMatrix[0]) or (NumberString == OddMatrix[1]) or (NumberString == OddMatrix[2]) or (NumberString == OddMatrix[3]) or (NumberString == OddMatrix[4]):
display(NumberString)
print('The NumberString is odd.')
else:
print('wtf is going on?')
which, funnily enough, also doesn't work. I've done something similar with numbers before and it's worked fine but I'm not sure when it comes to strings. Please help.
CodePudding user response:
You should iterate through the odd and even matrices and check if the number string is there in the matrices. Also, compare the strings in matrices using str() method/constructor because the members of the matrix in "sympy" are converted into symbols even if you provide them as strings. I guess the display() is your own method. The solution code could be;
NumberString = "four"
EvenMatrix = Matrix(["two", "four", "six", "eight", "ten"])
OddMatrix = Matrix(["one", "three", "five", "seven", "nine"])
even = False
odd = False
for numberEven in EvenMatrix:
if NumberString is str(numberEven):
print('The NumberString is even.')
even = True
for numberOdd in OddMatrix:
if NumberString is str(numberOdd):
print('The NumberString is odd.')
odd = True
if even is False and odd is False:
print("Wtf is going on")
CodePudding user response:
Seems to me that this is better handled with Python lists:
In [1]: NumberString = "four"
In [2]: evens=["two" ,"four" ,"six","eight" ,"ten"]
In [3]: odds=["one", "three", "five", "seven", "nine"]
In [4]: if NumberString in evens: print('even')
even
In [5]: if NumberString in evens: print('even')
...: elif NumberString in odds: print('odds')
...: else:print('wtf')
even
In [9]: NumberString='foo'
In [10]: if NumberString in evens: print('even')
...: elif NumberString in odds: print('odds')
...: else:print('wtf')
wtf
lists also have index
method:
In [12]: evens.index('four')
Out[12]: 1
In [13]: odds.index('four')
Traceback (most recent call last):
Input In [13] in <cell line: 1>
odds.index('four')
ValueError: 'four' is not in list
With numpy
arrays we can do:
In [14]: np.array(evens)=='four'
Out[14]: array([False, True, False, False, False])
In [15]: np.nonzero(np.array(evens)=='four')
Out[15]: (array([1]),)
One problem with the sympy
Matrix, is that the elements are not strings, but rather symbols
.
In [86]: EvenMatrix
Out[86]:
⎡ two ⎤
⎢ ⎥
⎢four ⎥
⎢ ⎥
⎢ six ⎥
⎢ ⎥
⎢eight⎥
⎢ ⎥
⎣ ten ⎦
In [88]: type(EvenMatrix[1])
Out[88]: sympy.core.symbol.Symbol
sympy
requires some care to distinguish between strings, Python variables, and sympy symbols. Even
four = sympy.symbols('four')
uses all 3 in different ways.