症状:属性错误:'列表;对象没有属性?

2022-04-06 00:00:00 python sympy

问题描述

我应该如何修复它

https://docs.sympy.org/latest/tutorial/basic_operations.html

from sympy import *
var('ax')
ans_a=ax
print("#",ans_a)
myValue={ax:6}
print("#",ans_a.subs(myValue))
#
var('bx')
ans_b=[bx]
print("#",ans_b)
myValue={bx:7}
print("#",ans_b.subs(myValue))
# ax
# 6
# [bx]
# AttributeError: 'list' object has no attribute 'subs'
# I want
# [7]

AttributeError: 'str' object has no attribute 'subs'

https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions


解决方案

我搜索了关于Subs的内容

from sympy import *
var('bx')
ans_b=[bx]
print("#",ans_b)
myValue={bx:7}
print("#", [ans.subs(myValue) for ans in ans_b])
#
var('m11 m12 m21 m22')
ans_M= Matrix([[m11,m12], [m21,m22]])
print("#",ans_M)
myValue={m11:11,m12:12,m21:21,m22:22}
print("#",ans_M.subs(myValue))
#
var('px py')
ans_P=Point(px,py)
print("#",ans_P)
myValue={px:1,py:2}
print("#",ans_P.subs(myValue))
# [bx]
# [7]
# Matrix([[m11, m12], [m21, m22]])
# Matrix([[11, 12], [21, 22]])
# Point2D(px, py)
# Point2D(1, 2)

相关文章