How do I get variables sorted?

56 views
Skip to first unread message

Rick Richardson

unread,
Oct 13, 2019, 8:54:23 PM10/13/19
to sympy
For example:

>>> a,b,e,c,f,d,g,h,i,j,k,l,m,n,o,p = symbols("a b e c f d g h i j k l m n o p ", integer=True)
>>> print solve([a-(1),a+b-(17),a+e-(20),b+c-(19),b+f-(22),c+d-(21),c+g-(24),d+h-(26),e+f-(25),e+i-(12),f+g-(27),f+j-(14),g+h-(29),g+k-(16),h+l-(18),i+j-(1),i+m-(4),j+k-(3),j+n-(6),k+l-(5),k+o-(8),l+p-(10),m+n-(9),n+o-(11),o+p-(13)],[a,b,e,c,f,d,g,h,i,j,k,l,m,n,o,p])

{f: 6, h: 8, k: -5, l: 10, o: 13, c: 3, i: -7, d: 18, g: 21, j: 8, m: 11, n: -2, a: 1, b: 16, e: 19, p: 0}

They come out in random order!

Chris Smith

unread,
Oct 14, 2019, 12:17:14 PM10/14/19
to sympy
It a dictionary. You can look at the results in order with

ans = solve(...)
for k in ordered(ans):
   
print(k, ans[k])


You could also turn that dict into an ordered list with `ans = list(ordered(ans.items()))`.

David Bailey

unread,
Oct 14, 2019, 12:57:23 PM10/14/19
to sy...@googlegroups.com
--

Just out of curiosity, will that solution execute ordered(ans) once or every turn of the loop?

David


Rick Richardson

unread,
Oct 14, 2019, 3:53:15 PM10/14/19
to sympy


On Monday, October 14, 2019 at 11:17:14 AM UTC-5, Chris Smith wrote:

You could also turn that dict into an ordered list with `ans = list(ordered(ans.items()))`.


 Thanks!

print list(ordered(ans.items()))

did the trick.

$ geo-algebraic-expressions -i a=1 a+b=17 a+e=20 b+c=19 b+f=22 c+d=21 c+g=24 d+h=26 e+f=25 e+i=12 f+g=27 f+j=14 g+h=29 g+k=16 h+l=18 i+j=1 i+m=4 j+k=3 j+n=6 k+l=5 k+o=8 l+p=10 m+n=9 n+o=11 o+p=13
[(a, 1), (b, 16), (c, 3), (d, 18), (e, 19), (f, 6), (g, 21), (h, 8), (i, -7), (j, 8), (k, -5), (l, 10), (m, 11), (n, -2), (o, 13), (p, 0)]


Aaron Meurer

unread,
Oct 15, 2019, 11:56:03 AM10/15/19
to sympy
The iterator part of a for loop is only executed once.

Aaron Meurer

>
> David
>
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/ccd193a0-9c50-dd5d-4ffd-fd8a1baaa96e%40dbailey.co.uk.
Reply all
Reply to author
Forward
0 new messages