Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sympy: list comprehension how to loop.multilist?

sympy: list comprehension how to loop.multilist ?

Please tell me how to loop.

Do you need 14 lines?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading