how to replace a comma â,â with â||â in a list
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a list of equations:
dene = x1== 0.2, x2== 0.4, x3==0.5;
I like to convert this list to a Rule:
iki=x3==0.5]//Flatten
which works. Since my list of equations is a very long list, I like to use Mathematica function.
I have two options to make "dene" a Rule: either Replace "==" with "->" or Substitute "," with "||". I could not do any of these options, although I spent sometime on the problem. I need your help.
replacement
New contributor
add a comment |Â
up vote
3
down vote
favorite
I have a list of equations:
dene = x1== 0.2, x2== 0.4, x3==0.5;
I like to convert this list to a Rule:
iki=x3==0.5]//Flatten
which works. Since my list of equations is a very long list, I like to use Mathematica function.
I have two options to make "dene" a Rule: either Replace "==" with "->" or Substitute "," with "||". I could not do any of these options, although I spent sometime on the problem. I need your help.
replacement
New contributor
6
ToRules[Or @@ x1 == 0.2, x2 == 0.4, x3 == 0.5]
?
â J. M. is somewhat okay.â¦
4 hours ago
4
Rule @@@ x1 == 0.2, x2 == 0.4, x3 == 0.5
?
â AccidentalFourierTransform
4 hours ago
Both answers work for me, though @Accidenta's answer is more compact.
â Tebernus
2 hours ago
1
I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g.,;
which is short forCompoundExpression
). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, useSequence
.
â Sjoerd Smit
1 hour ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a list of equations:
dene = x1== 0.2, x2== 0.4, x3==0.5;
I like to convert this list to a Rule:
iki=x3==0.5]//Flatten
which works. Since my list of equations is a very long list, I like to use Mathematica function.
I have two options to make "dene" a Rule: either Replace "==" with "->" or Substitute "," with "||". I could not do any of these options, although I spent sometime on the problem. I need your help.
replacement
New contributor
I have a list of equations:
dene = x1== 0.2, x2== 0.4, x3==0.5;
I like to convert this list to a Rule:
iki=x3==0.5]//Flatten
which works. Since my list of equations is a very long list, I like to use Mathematica function.
I have two options to make "dene" a Rule: either Replace "==" with "->" or Substitute "," with "||". I could not do any of these options, although I spent sometime on the problem. I need your help.
replacement
replacement
New contributor
New contributor
New contributor
asked 4 hours ago
Tebernus
306
306
New contributor
New contributor
6
ToRules[Or @@ x1 == 0.2, x2 == 0.4, x3 == 0.5]
?
â J. M. is somewhat okay.â¦
4 hours ago
4
Rule @@@ x1 == 0.2, x2 == 0.4, x3 == 0.5
?
â AccidentalFourierTransform
4 hours ago
Both answers work for me, though @Accidenta's answer is more compact.
â Tebernus
2 hours ago
1
I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g.,;
which is short forCompoundExpression
). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, useSequence
.
â Sjoerd Smit
1 hour ago
add a comment |Â
6
ToRules[Or @@ x1 == 0.2, x2 == 0.4, x3 == 0.5]
?
â J. M. is somewhat okay.â¦
4 hours ago
4
Rule @@@ x1 == 0.2, x2 == 0.4, x3 == 0.5
?
â AccidentalFourierTransform
4 hours ago
Both answers work for me, though @Accidenta's answer is more compact.
â Tebernus
2 hours ago
1
I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g.,;
which is short forCompoundExpression
). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, useSequence
.
â Sjoerd Smit
1 hour ago
6
6
ToRules[Or @@ x1 == 0.2, x2 == 0.4, x3 == 0.5]
?â J. M. is somewhat okay.â¦
4 hours ago
ToRules[Or @@ x1 == 0.2, x2 == 0.4, x3 == 0.5]
?â J. M. is somewhat okay.â¦
4 hours ago
4
4
Rule @@@ x1 == 0.2, x2 == 0.4, x3 == 0.5
?â AccidentalFourierTransform
4 hours ago
Rule @@@ x1 == 0.2, x2 == 0.4, x3 == 0.5
?â AccidentalFourierTransform
4 hours ago
Both answers work for me, though @Accidenta's answer is more compact.
â Tebernus
2 hours ago
Both answers work for me, though @Accidenta's answer is more compact.
â Tebernus
2 hours ago
1
1
I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g.,
;
which is short for CompoundExpression
). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, use Sequence
.â Sjoerd Smit
1 hour ago
I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g.,
;
which is short for CompoundExpression
). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, use Sequence
.â Sjoerd Smit
1 hour ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.
x1 == 0.2, x2 == 0.4, x3 == 0.5 /. Equal -> Rule
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
A multi-level example
Array[x[##] == ## &, 2, 2]
% /. Equal -> Rule
x[1, 1] == 1, 1, x[1, 2] == 1, 2, x[2, 1] == 2, 1,
x[2, 2] == 2, 2
x[1, 1] -> 1, 1, x[1, 2] -> 1, 2, x[2, 1] -> 2, 1,
x[2, 2] -> 2, 2
add a comment |Â
up vote
4
down vote
Here is a simple function that does what I think you want.
makeRules[p : _Equal ..] := Rule @@@ p
Then
dene = x1 == 0.2, x2 == 0.4, x3 == 0.5;
makeRules[dene]
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
@Tebernus. In practice, it will often be simpler just to writeRule @@@ ...
but you said in your question that you would like a function.
â m_goldberg
2 hours ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
add a comment |Â
up vote
3
down vote
This is a good use for Cases
with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or
expressions:
expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;
Cases[expr, lhs_ == rhs_ :> lhs -> rhs, Infinity]
(* x1 -> 0.2, x2 -> 0.4, x3 -> 0.5 *)
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.
x1 == 0.2, x2 == 0.4, x3 == 0.5 /. Equal -> Rule
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
A multi-level example
Array[x[##] == ## &, 2, 2]
% /. Equal -> Rule
x[1, 1] == 1, 1, x[1, 2] == 1, 2, x[2, 1] == 2, 1,
x[2, 2] == 2, 2
x[1, 1] -> 1, 1, x[1, 2] -> 1, 2, x[2, 1] -> 2, 1,
x[2, 2] -> 2, 2
add a comment |Â
up vote
1
down vote
accepted
Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.
x1 == 0.2, x2 == 0.4, x3 == 0.5 /. Equal -> Rule
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
A multi-level example
Array[x[##] == ## &, 2, 2]
% /. Equal -> Rule
x[1, 1] == 1, 1, x[1, 2] == 1, 2, x[2, 1] == 2, 1,
x[2, 2] == 2, 2
x[1, 1] -> 1, 1, x[1, 2] -> 1, 2, x[2, 1] -> 2, 1,
x[2, 2] -> 2, 2
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.
x1 == 0.2, x2 == 0.4, x3 == 0.5 /. Equal -> Rule
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
A multi-level example
Array[x[##] == ## &, 2, 2]
% /. Equal -> Rule
x[1, 1] == 1, 1, x[1, 2] == 1, 2, x[2, 1] == 2, 1,
x[2, 2] == 2, 2
x[1, 1] -> 1, 1, x[1, 2] -> 1, 2, x[2, 1] -> 2, 1,
x[2, 2] -> 2, 2
Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.
x1 == 0.2, x2 == 0.4, x3 == 0.5 /. Equal -> Rule
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
A multi-level example
Array[x[##] == ## &, 2, 2]
% /. Equal -> Rule
x[1, 1] == 1, 1, x[1, 2] == 1, 2, x[2, 1] == 2, 1,
x[2, 2] == 2, 2
x[1, 1] -> 1, 1, x[1, 2] -> 1, 2, x[2, 1] -> 2, 1,
x[2, 2] -> 2, 2
answered 31 mins ago
That Gravity Guy
1,047512
1,047512
add a comment |Â
add a comment |Â
up vote
4
down vote
Here is a simple function that does what I think you want.
makeRules[p : _Equal ..] := Rule @@@ p
Then
dene = x1 == 0.2, x2 == 0.4, x3 == 0.5;
makeRules[dene]
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
@Tebernus. In practice, it will often be simpler just to writeRule @@@ ...
but you said in your question that you would like a function.
â m_goldberg
2 hours ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
add a comment |Â
up vote
4
down vote
Here is a simple function that does what I think you want.
makeRules[p : _Equal ..] := Rule @@@ p
Then
dene = x1 == 0.2, x2 == 0.4, x3 == 0.5;
makeRules[dene]
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
@Tebernus. In practice, it will often be simpler just to writeRule @@@ ...
but you said in your question that you would like a function.
â m_goldberg
2 hours ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Here is a simple function that does what I think you want.
makeRules[p : _Equal ..] := Rule @@@ p
Then
dene = x1 == 0.2, x2 == 0.4, x3 == 0.5;
makeRules[dene]
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
Here is a simple function that does what I think you want.
makeRules[p : _Equal ..] := Rule @@@ p
Then
dene = x1 == 0.2, x2 == 0.4, x3 == 0.5;
makeRules[dene]
x1 -> 0.2, x2 -> 0.4, x3 -> 0.5
answered 2 hours ago
m_goldberg
82.3k869190
82.3k869190
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
@Tebernus. In practice, it will often be simpler just to writeRule @@@ ...
but you said in your question that you would like a function.
â m_goldberg
2 hours ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
add a comment |Â
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
@Tebernus. In practice, it will often be simpler just to writeRule @@@ ...
but you said in your question that you would like a function.
â m_goldberg
2 hours ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution.
â Tebernus
2 hours ago
@Tebernus. In practice, it will often be simpler just to write
Rule @@@ ...
but you said in your question that you would like a function.â m_goldberg
2 hours ago
@Tebernus. In practice, it will often be simpler just to write
Rule @@@ ...
but you said in your question that you would like a function.â m_goldberg
2 hours ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus
â Tebernus
51 mins ago
add a comment |Â
up vote
3
down vote
This is a good use for Cases
with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or
expressions:
expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;
Cases[expr, lhs_ == rhs_ :> lhs -> rhs, Infinity]
(* x1 -> 0.2, x2 -> 0.4, x3 -> 0.5 *)
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
add a comment |Â
up vote
3
down vote
This is a good use for Cases
with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or
expressions:
expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;
Cases[expr, lhs_ == rhs_ :> lhs -> rhs, Infinity]
(* x1 -> 0.2, x2 -> 0.4, x3 -> 0.5 *)
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
This is a good use for Cases
with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or
expressions:
expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;
Cases[expr, lhs_ == rhs_ :> lhs -> rhs, Infinity]
(* x1 -> 0.2, x2 -> 0.4, x3 -> 0.5 *)
This is a good use for Cases
with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or
expressions:
expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;
Cases[expr, lhs_ == rhs_ :> lhs -> rhs, Infinity]
(* x1 -> 0.2, x2 -> 0.4, x3 -> 0.5 *)
answered 4 hours ago
Pillsy
12.2k13178
12.2k13178
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
add a comment |Â
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
This is a good answer for pattern recognition.
â Tebernus
2 hours ago
add a comment |Â
Tebernus is a new contributor. Be nice, and check out our Code of Conduct.
Tebernus is a new contributor. Be nice, and check out our Code of Conduct.
Tebernus is a new contributor. Be nice, and check out our Code of Conduct.
Tebernus is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182769%2fhow-to-replace-a-comma-with-in-a-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
6
ToRules[Or @@ x1 == 0.2, x2 == 0.4, x3 == 0.5]
?â J. M. is somewhat okay.â¦
4 hours ago
4
Rule @@@ x1 == 0.2, x2 == 0.4, x3 == 0.5
?â AccidentalFourierTransform
4 hours ago
Both answers work for me, though @Accidenta's answer is more compact.
â Tebernus
2 hours ago
1
I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g.,
;
which is short forCompoundExpression
). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, useSequence
.â Sjoerd Smit
1 hour ago