【问题标题】:Understand how xreplace worksUnderstand how xreplace works
【发布时间】:2022-12-01 18:33:33
【问题描述】:

I'm trying to figure out how xreplace works. When I write

    from sympy import *
    my_list = [1234.5678, 22.333333]
    my_list.xreplace({n : round(n, 4) for n in expr.atoms(Number)})

I get an error:'list' object has no attribute 'xreplace'. What did I write wrong?

【问题讨论】:

    标签: python sympy


    【解决方案1】:

    xreplace is a method of SymPy's objects. Your my_list is just an ordinary Python list, so it doesn't expose that method. What you can do it this:

    from sympy import *
    # Tuple is the SymPy version of a python tuple
    my_list = Tuple(1234.5678, 22.333333)
    my_list.xreplace({n : round(n, 4) for n in my_list.atoms(Number)})
    

    【讨论】:

      猜你喜欢
      • 2022-12-19
      • 2022-12-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 2022-12-02
      • 2022-12-02
      • 1970-01-01
      相关资源
      最近更新 更多