Repeated replacing elements in the list
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a large list of the following form
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1
and I want to combine all of the pairs x->y that share the same x value by adding up their y values. For the above example I want to have
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
and I want to do it using patterns and ReplaceRepeated command. I tried the following code:
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //.HoldPattern[x_ -> y_, a___, x_ -> z_] -> x -> y + z, a
Can anybody tell me why above code is not working and how can I do it using rules?
pattern-matching replacement
New contributor
add a comment |Â
up vote
1
down vote
favorite
I have a large list of the following form
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1
and I want to combine all of the pairs x->y that share the same x value by adding up their y values. For the above example I want to have
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
and I want to do it using patterns and ReplaceRepeated command. I tried the following code:
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //.HoldPattern[x_ -> y_, a___, x_ -> z_] -> x -> y + z, a
Can anybody tell me why above code is not working and how can I do it using rules?
pattern-matching replacement
New contributor
1
//.
is not really intended for combinations like these. TryMerge
:Normal[Merge[1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1, Total]]
â J. M. is somewhat okay.â¦
1 hour ago
Thanks @J.M.issomewhatokay. You answer is correct. However, I mostly want to learn about patterns and I wanna know how can extract such patterns.
â The Legend of 1991
1 hour ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a large list of the following form
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1
and I want to combine all of the pairs x->y that share the same x value by adding up their y values. For the above example I want to have
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
and I want to do it using patterns and ReplaceRepeated command. I tried the following code:
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //.HoldPattern[x_ -> y_, a___, x_ -> z_] -> x -> y + z, a
Can anybody tell me why above code is not working and how can I do it using rules?
pattern-matching replacement
New contributor
I have a large list of the following form
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1
and I want to combine all of the pairs x->y that share the same x value by adding up their y values. For the above example I want to have
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
and I want to do it using patterns and ReplaceRepeated command. I tried the following code:
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //.HoldPattern[x_ -> y_, a___, x_ -> z_] -> x -> y + z, a
Can anybody tell me why above code is not working and how can I do it using rules?
pattern-matching replacement
pattern-matching replacement
New contributor
New contributor
New contributor
asked 1 hour ago
The Legend of 1991
82
82
New contributor
New contributor
1
//.
is not really intended for combinations like these. TryMerge
:Normal[Merge[1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1, Total]]
â J. M. is somewhat okay.â¦
1 hour ago
Thanks @J.M.issomewhatokay. You answer is correct. However, I mostly want to learn about patterns and I wanna know how can extract such patterns.
â The Legend of 1991
1 hour ago
add a comment |Â
1
//.
is not really intended for combinations like these. TryMerge
:Normal[Merge[1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1, Total]]
â J. M. is somewhat okay.â¦
1 hour ago
Thanks @J.M.issomewhatokay. You answer is correct. However, I mostly want to learn about patterns and I wanna know how can extract such patterns.
â The Legend of 1991
1 hour ago
1
1
//.
is not really intended for combinations like these. Try Merge
: Normal[Merge[1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1, Total]]
â J. M. is somewhat okay.â¦
1 hour ago
//.
is not really intended for combinations like these. Try Merge
: Normal[Merge[1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1, Total]]
â J. M. is somewhat okay.â¦
1 hour ago
Thanks @J.M.issomewhatokay. You answer is correct. However, I mostly want to learn about patterns and I wanna know how can extract such patterns.
â The Legend of 1991
1 hour ago
Thanks @J.M.issomewhatokay. You answer is correct. However, I mostly want to learn about patterns and I wanna know how can extract such patterns.
â The Legend of 1991
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
In general, there may (or may not) be preceding, intervening, and subsequent elements in the list that must handled using BlankNullSequence
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //. start___,
x_ -> y1_, middle___, x_ -> y2_, end___ :> start, x -> y1 + y2, middle, end
(* 1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1 *)
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
add a comment |Â
up vote
1
down vote
You could use GroupBy
:
Normal @ GroupBy[
1,2->-1,1,1->1,1,2->-1,1,6->1,
First -> Last,
Total
]
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In general, there may (or may not) be preceding, intervening, and subsequent elements in the list that must handled using BlankNullSequence
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //. start___,
x_ -> y1_, middle___, x_ -> y2_, end___ :> start, x -> y1 + y2, middle, end
(* 1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1 *)
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
add a comment |Â
up vote
1
down vote
accepted
In general, there may (or may not) be preceding, intervening, and subsequent elements in the list that must handled using BlankNullSequence
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //. start___,
x_ -> y1_, middle___, x_ -> y2_, end___ :> start, x -> y1 + y2, middle, end
(* 1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1 *)
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In general, there may (or may not) be preceding, intervening, and subsequent elements in the list that must handled using BlankNullSequence
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //. start___,
x_ -> y1_, middle___, x_ -> y2_, end___ :> start, x -> y1 + y2, middle, end
(* 1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1 *)
In general, there may (or may not) be preceding, intervening, and subsequent elements in the list that must handled using BlankNullSequence
1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1 //. start___,
x_ -> y1_, middle___, x_ -> y2_, end___ :> start, x -> y1 + y2, middle, end
(* 1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1 *)
answered 55 mins ago
Bob Hanlon
55.8k23589
55.8k23589
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
add a comment |Â
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Because it is only coincidental that the first element of your list is one of interest (i.e., matching LHS of rule). In general, any number of non-matching elements could be at the start.
â Bob Hanlon
35 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
Thanks Bob! I am still having problem with start and end. As a related question can you please tell me why for instance Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y] command doesn't return the y values that are greater than their neighbors in the list?
â The Legend of 1991
15 mins ago
add a comment |Â
up vote
1
down vote
You could use GroupBy
:
Normal @ GroupBy[
1,2->-1,1,1->1,1,2->-1,1,6->1,
First -> Last,
Total
]
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
add a comment |Â
up vote
1
down vote
You could use GroupBy
:
Normal @ GroupBy[
1,2->-1,1,1->1,1,2->-1,1,6->1,
First -> Last,
Total
]
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could use GroupBy
:
Normal @ GroupBy[
1,2->-1,1,1->1,1,2->-1,1,6->1,
First -> Last,
Total
]
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
You could use GroupBy
:
Normal @ GroupBy[
1,2->-1,1,1->1,1,2->-1,1,6->1,
First -> Last,
Total
]
1, 2 -> -2, 1, 1 -> 1, 1, 6 -> 1
answered 50 mins ago
Carl Woll
59k277152
59k277152
add a comment |Â
add a comment |Â
The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.
The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.
The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.
The Legend of 1991 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%2f182847%2frepeated-replacing-elements-in-the-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
1
//.
is not really intended for combinations like these. TryMerge
:Normal[Merge[1, 2 -> -1, 1, 1 -> 1, 1, 2 -> -1, 1, 6 -> 1, Total]]
â J. M. is somewhat okay.â¦
1 hour ago
Thanks @J.M.issomewhatokay. You answer is correct. However, I mostly want to learn about patterns and I wanna know how can extract such patterns.
â The Legend of 1991
1 hour ago