As you can see from the picture, how can I change all the "->" symbols to "-"?
3 Answers
$\begingroup$
$\endgroup$
5
Look at the FullForm
of a Rule
a -> b // FullForm
(* Rule[a, b] *)
SeedRandom[1234];
b3 = Thread[{a, b, c, d, e} -> RandomInteger[100, 5]]
(* {a -> 8, b -> 72, c -> 44, d -> 38, e -> 22} *)
b3 /. Rule[a_, b_] :> StringForm["``-``", a, b]
or with some space
b3 /. Rule[a_, b_] :> StringForm["``\[ThinSpace]-\[ThinSpace]``", a, b]
-
$\begingroup$ Thanks for your answer, Mr Hanlon, I completely got the "-" symbol, but I faced another trouble. How can I transform the output into dataset? I tried to use Dataset function, but it showed lots of symbols different from the output. $\endgroup$ Commented Mar 22, 2021 at 7:24
-
$\begingroup$
b3 = Thread[{a, b, c, d, e} -> RandomInteger[100, 5]]; Dataset[Association[b3]]
$\endgroup$ Commented Mar 22, 2021 at 7:30 -
$\begingroup$ Oh! I just got the same anser with Dataset function, there are lots of mess symbols. $\endgroup$ Commented Mar 22, 2021 at 7:46
-
$\begingroup$ Post a question showing data (for a small sample) and your code. Note that you cannot be be using the transformation of the
Rule
shown above since the rules must remain to form the association. $\endgroup$ Commented Mar 22, 2021 at 7:50 -
$\begingroup$ For example: c1 = {x, y, z};c2 = {1, 2, 3};c3 = Thread[c1 -> c2] I want to get the dataset of c3, and when I just use Dataset function, it shows "->" symbol, I want to replace it with symbol "-". $\endgroup$ Commented Mar 22, 2021 at 8:25
$\begingroup$
$\endgroup$
3
a->b
is a shortcut for Rule[a,b]
. Therefore, what you have to do is to replace Rule
by Subtract
. E.g.:
SeedRandom[1]
list = Thread[{a, b, c, d, e} -> RandomInteger[100, 5]]
(* {a -> 80, b -> 14, c -> 0, d -> 67, e -> 3} *)
list /. Rule -> Subtract
(* {-80 + a, -14 + b, c, -67 + d, -3 + e} *)
-
$\begingroup$ Thanks for your answer, Mr Huber, but I want to get the "-" as only symbol but ont subtract. $\endgroup$ Commented Mar 22, 2021 at 7:20
-
$\begingroup$ If you want a string form:
StringReplace[ToString[list], "->" -> "-"]
$\endgroup$ Commented Mar 22, 2021 at 10:42 -
$\begingroup$ This is a good method, but I want to get a dataset in the end. $\endgroup$ Commented Mar 22, 2021 at 14:19
$\begingroup$
$\endgroup$
rules = {a -> 26, b -> 45, c -> 32, d -> 56, e -> 2};
Using StringRiffle
StringRiffle[#, "-"] & /@ List @@@ rules
{"a-26", "b-45", "c-32", "d-56", "e-2"}