Repeated replacing elements in the list

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question







New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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










  • 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














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?










share|improve this question







New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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










  • 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












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?










share|improve this question







New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









The Legend of 1991

82




82




New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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










  • 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




    //. 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







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










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 *)





share|improve this answer




















  • 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

















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







share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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 *)





    share|improve this answer




















    • 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














    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 *)





    share|improve this answer




















    • 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












    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 *)





    share|improve this answer












    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 *)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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
















    • 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










    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







    share|improve this answer
























      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







      share|improve this answer






















        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







        share|improve this answer












        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








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 50 mins ago









        Carl Woll

        59k277152




        59k277152




















            The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery