Say I have a directed graph inputted in the following format in Mathematica:
g = {{1, "a", 2}, {2, "b", 1}}
I'd like to plot this graph using GraphPlot
in Mathematica, which is the following syntax:
GraphPlot[{{1 -> 2, "a"}, {2 -> 1, "b"}}, VertexLabeling -> True, DirectedEdges -> True,
VertexCoordinateRules -> Auto, VertexLabeling -> True]
How would I swap the middle elements and the last elements in any list like g
, so that g
would look closer to the argument of GraphPlot
?
{{1 -> 2, "a"}, {2 -> 1, "b"}}
I have searched the Mathematica documentation but was unable to find a good way of doing this.
g /. {f_Integer, m_String, l_Integer} :> {f -> l, m}
$\endgroup$g[[All, 1]] = Rule @@@ g[[All, {1, 3}]];g[[All, {1, 2}]]
$\endgroup$