1
$\begingroup$

I've a few python data types

edges = [(1,2), (1,3), (1,4), (2,5), (2,6), (5,6), (3,4), (3,7), (6,7), (7,8), (2,9)];
vl = [1,2,3,4,5,6,7,8,9]
ew = {(1, 2) : 49.6, (1,3): 74.4, (1, 4) : 49.6, (2,5): 37.2, (2, 6) : 74.4, (5, 6): 49.6, (3, 4) : 37.2, (3, 7):24.8, (6, 7) : 62, (7, 8) : 37.2, (2, 9) : 24.8}
vd = {1:[75., 25., 0], 2:[115., 45., 0], 3:[10., 5., 0], 4:[45., 0, 0], 5:[90., 60., 0], 6:[45., 55., 0], 7:[0, 25., 0], 8:[10., 50.,0], 9:[115.,  25.,0]};

I want to pass these variables to a function that will convert these to Mathematica expression. I want to do something like below,

solutiond[edges_, vd_, vl_, ew_] := (
  {edges, vl, ew, vd} = 
    ImportString[#, "PythonExpression"]& /@ {"edges","vl","ew","vd"};)

solutiond[edges, vd, vl, ew]

But I am not sure how to pass these input arguments here

{"edges", "vl", "ew", "vd"}

In python I use .format.

Suggestions will be helpful.

$\endgroup$
1
  • $\begingroup$ Any suggestions? $\endgroup$ – Natasha Sep 29 '20 at 10:03
2
$\begingroup$

Each time you call ImportString[..., "PythonExpression"] Mathematica run it as an isolated environment:

ImportString["a=2", "PythonExpression"]

ImportString["a", "PythonExpression"] (* ERROR *)

The above code will raise an error because there is no defined name as a.

Solution 1

what you could do is to start a ExternalSession, run your code in python then ask python to return each variable value:

session = StartExternalSession["Python"];

ExternalEvaluate[session, 
  "edges = [(1,2), (1,3), (1,4), (2,5), (2,6), (5,6), (3,4), (3,7), (6,7), (7,8), (2,9)];
vl = [1,2,3,4,5,6,7,8,9];
ew = {(1, 2) : 49.6, (1,3): 74.4, (1, 4) : 49.6, (2,5): 37.2, (2, 6) : 74.4, (5, 6): 49.6, (3, 4) : 37.2, (3, 7):24.8, (6, 7) : 62, (7, 8) : 37.2, (2, 9) : 24.8}
vd = {1:[75., 25., 0], 2:[115., 45., 0], 3:[10., 5., 0], 4:[45., 0, 0], 5:[90., 60., 0], 6:[45., 55., 0], 7:[0, 25., 0], 8:[10., 50.,0], 9:[115.,  25.,0]};"];

ExternalEvaluate[session, #] & /@ {"edges", "vl", "ew", "vd"}

DeleteObject[session];

Output:

{{{1, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {5, 6}, {3, 4}, {3, 7}, {6, 
   7}, {7, 8}, {2, 9}}, {1, 2, 3, 4, 5, 6, 7, 8, 
  9}, <|{1, 2} -> 49.6, {1, 3} -> 74.4, {1, 4} -> 49.6, {2, 5} -> 
   37.2, {2, 6} -> 74.4, {5, 6} -> 49.6, {3, 4} -> 37.2, {3, 7} -> 
   24.8, {6, 7} -> 62, {7, 8} -> 37.2, {2, 9} -> 
   24.8|>, <|1 -> {75., 25., 0}, 2 -> {115., 45., 0}, 
  3 -> {10., 5., 0}, 4 -> {45., 0, 0}, 5 -> {90., 60., 0}, 
  6 -> {45., 55., 0}, 7 -> {0, 25., 0}, 8 -> {10., 50., 0}, 
  9 -> {115., 25., 0}|>}

Solution 2

You could still use ImportString to import directly but your python code needs to be changed:

ImportString["[(1,2), (1,3), (1,4), (2,5), (2,6), (5,6), (3,4), (3,7), (6,7), (7,8), (2,9)]", "PythonExpression"]

Out:

{{1, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {5, 6}, {3, 4}, {3, 7}, {6, 7}, {7, 8}, {2, 9}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.