如何将字符串变量与 Sympy 矩阵中的字符串变量列表进行比较?

How do I compare a string variable to a list of string variables in a Sympy matrix?

我正在尝试访问矩阵中的字符串变量列表,以与 if/elif 语句中用户输入的字符串进行比较。我似乎无法获得访问权限,因为我的代码打印了 'else' 语句并且没有按照我的意愿执行...

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?')

我想将用户输入的字符串与矩阵中的字符串列表进行比较,以获得更清晰的代码,而不是像这样:

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?')

有趣的是,这也行不通。我以前对数字做过类似的事情,而且效果很好,但我不确定什么时候涉及到字符串。请帮忙

在我看来,使用 Python 列表可以更好地处理这个问题:

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

列表也有index方法:

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

使用 numpy 个数组我们可以做:

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]),)

sympy 矩阵的一个问题是元素不是字符串,而是 symbols

In [86]: EvenMatrix
Out[86]: 
⎡ two ⎤
⎢     ⎥
⎢four ⎥
⎢     ⎥
⎢ six ⎥
⎢     ⎥
⎢eight⎥
⎢     ⎥
⎣ ten ⎦

In [88]: type(EvenMatrix[1])
Out[88]: sympy.core.symbol.Symbol

sympy 需要注意区分字符串、Python 变量和 sympy 符号。甚至

four = sympy.symbols('four')

以不同的方式使用全部 3 个。

您应该遍历奇偶矩阵并检查矩阵中是否存在数字字符串。此外,使用 str() method/constructor 比较矩阵中的字符串,因为“sympy”中矩阵的成员被转换为符号,即使您将它们作为字符串提供。我猜 display() 是你自己的方法。解决方案代码可以是;

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")