Home > Software engineering >  sympy: list comprehension how to loop.multilist?
sympy: list comprehension how to loop.multilist?

Time:11-21

sympy: list comprehension how to loop.multilist ?

Please tell me how to loop.

Do you need 14 lines?

var('ax bx ~ nx ')
ans_ab~n=[[ax],[bx],~,[nx]]
myValue={ax:5,bx:7  ~ nx:14}
print("#",[[ans.subs(myValue) for ans in ans_ab[0]]\
          ,[ans.subs(myValue) for ans in ans_ab[1]]\
          ~
          ,[ans.subs(myValue) for ans in ans_ab[n]]]
)

python 3 double loop comprehension clarification

sympy:AttributeError: 'list' object has no attribute 'subs'?

num=2 OK

from sympy import *
var('ax bx')
ans_ab=[[ax],[bx]]
print("#",ans_ab)
myValue={ax:5,bx:7}
print("#",[[ans.subs(myValue) for ans in ans_ab[0]]
          ,[ans.subs(myValue) for ans in ans_ab[1]]])
# [[ax], [bx]]
# [[5], [7]]

CodePudding user response:

Assuming you want a list of lists, you should be able to do something like this:

answers = [
    [ans.subs(myValue) for ans in ans_ab[i]]
    for i in range(n)
]
print("#", answers)

If you want a flat list, you can do this:

answers = [
    ans.subs(myValue)
    for i in range(n)
    for ans in ans_ab[i]
]
print("#", answers)

CodePudding user response:

from sympy import *
var('ax bx')
ans_ab=[[ax],[bx]]
print("#",ans_ab)
myValue={ax:5,bx:7}
print("#", [[ans.subs(myValue) for ans in ans_ab[i]] for i in range(len(ans_ab))])
# [[ax], [bx]]
# [[5], [7]]
  • Related