How to change the first value in the tuple of a list?












1















This is my matrix:



b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 
0.023)]]


And I want to let it to be a



b = [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 
0.023)]]


To add n for the first value in the tuple, and I tried:



for n in b:
for ee,ww in n:
ee == ee + 2903


It doesn't work.
How should I keep the change to the original matrix b?










share|improve this question




















  • 2





    Tuples are immutable; at the least, you need to replace each tuple with a new one in each sublist.

    – chepner
    Nov 19 '18 at 16:29






  • 1





    I don't understand where the 2903 comes from, and b is three levels deep while your for loop is only two. Also you're using the comparison == rather than the assignment =.

    – Acccumulation
    Nov 19 '18 at 16:34











  • If I gave you a function update, would it have to update b in place, so that print(b); update(b); print(b) would output two different values, or can it return a copy new_b = update(b). In the second case, do you need to retain the original object? That is, after new_b = update(b), would you require id(new_b) == id(b)? Would you require that id(new_b[0]) == id(b[0]) as well? (Keeping the existing rows, and only replacing the tuples within each row?)

    – chepner
    Nov 19 '18 at 16:38













  • ee== .. is not an assignment. And ee=.. changes the variable but does not change n.

    – hpaulj
    Nov 19 '18 at 16:47
















1















This is my matrix:



b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 
0.023)]]


And I want to let it to be a



b = [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 
0.023)]]


To add n for the first value in the tuple, and I tried:



for n in b:
for ee,ww in n:
ee == ee + 2903


It doesn't work.
How should I keep the change to the original matrix b?










share|improve this question




















  • 2





    Tuples are immutable; at the least, you need to replace each tuple with a new one in each sublist.

    – chepner
    Nov 19 '18 at 16:29






  • 1





    I don't understand where the 2903 comes from, and b is three levels deep while your for loop is only two. Also you're using the comparison == rather than the assignment =.

    – Acccumulation
    Nov 19 '18 at 16:34











  • If I gave you a function update, would it have to update b in place, so that print(b); update(b); print(b) would output two different values, or can it return a copy new_b = update(b). In the second case, do you need to retain the original object? That is, after new_b = update(b), would you require id(new_b) == id(b)? Would you require that id(new_b[0]) == id(b[0]) as well? (Keeping the existing rows, and only replacing the tuples within each row?)

    – chepner
    Nov 19 '18 at 16:38













  • ee== .. is not an assignment. And ee=.. changes the variable but does not change n.

    – hpaulj
    Nov 19 '18 at 16:47














1












1








1


1






This is my matrix:



b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 
0.023)]]


And I want to let it to be a



b = [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 
0.023)]]


To add n for the first value in the tuple, and I tried:



for n in b:
for ee,ww in n:
ee == ee + 2903


It doesn't work.
How should I keep the change to the original matrix b?










share|improve this question
















This is my matrix:



b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 
0.023)]]


And I want to let it to be a



b = [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 
0.023)]]


To add n for the first value in the tuple, and I tried:



for n in b:
for ee,ww in n:
ee == ee + 2903


It doesn't work.
How should I keep the change to the original matrix b?







python numpy replace tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:29









jdehesa

23.8k43554




23.8k43554










asked Nov 19 '18 at 16:27









賴韋安賴韋安

142




142








  • 2





    Tuples are immutable; at the least, you need to replace each tuple with a new one in each sublist.

    – chepner
    Nov 19 '18 at 16:29






  • 1





    I don't understand where the 2903 comes from, and b is three levels deep while your for loop is only two. Also you're using the comparison == rather than the assignment =.

    – Acccumulation
    Nov 19 '18 at 16:34











  • If I gave you a function update, would it have to update b in place, so that print(b); update(b); print(b) would output two different values, or can it return a copy new_b = update(b). In the second case, do you need to retain the original object? That is, after new_b = update(b), would you require id(new_b) == id(b)? Would you require that id(new_b[0]) == id(b[0]) as well? (Keeping the existing rows, and only replacing the tuples within each row?)

    – chepner
    Nov 19 '18 at 16:38













  • ee== .. is not an assignment. And ee=.. changes the variable but does not change n.

    – hpaulj
    Nov 19 '18 at 16:47














  • 2





    Tuples are immutable; at the least, you need to replace each tuple with a new one in each sublist.

    – chepner
    Nov 19 '18 at 16:29






  • 1





    I don't understand where the 2903 comes from, and b is three levels deep while your for loop is only two. Also you're using the comparison == rather than the assignment =.

    – Acccumulation
    Nov 19 '18 at 16:34











  • If I gave you a function update, would it have to update b in place, so that print(b); update(b); print(b) would output two different values, or can it return a copy new_b = update(b). In the second case, do you need to retain the original object? That is, after new_b = update(b), would you require id(new_b) == id(b)? Would you require that id(new_b[0]) == id(b[0]) as well? (Keeping the existing rows, and only replacing the tuples within each row?)

    – chepner
    Nov 19 '18 at 16:38













  • ee== .. is not an assignment. And ee=.. changes the variable but does not change n.

    – hpaulj
    Nov 19 '18 at 16:47








2




2





Tuples are immutable; at the least, you need to replace each tuple with a new one in each sublist.

– chepner
Nov 19 '18 at 16:29





Tuples are immutable; at the least, you need to replace each tuple with a new one in each sublist.

– chepner
Nov 19 '18 at 16:29




1




1





I don't understand where the 2903 comes from, and b is three levels deep while your for loop is only two. Also you're using the comparison == rather than the assignment =.

– Acccumulation
Nov 19 '18 at 16:34





I don't understand where the 2903 comes from, and b is three levels deep while your for loop is only two. Also you're using the comparison == rather than the assignment =.

– Acccumulation
Nov 19 '18 at 16:34













If I gave you a function update, would it have to update b in place, so that print(b); update(b); print(b) would output two different values, or can it return a copy new_b = update(b). In the second case, do you need to retain the original object? That is, after new_b = update(b), would you require id(new_b) == id(b)? Would you require that id(new_b[0]) == id(b[0]) as well? (Keeping the existing rows, and only replacing the tuples within each row?)

– chepner
Nov 19 '18 at 16:38







If I gave you a function update, would it have to update b in place, so that print(b); update(b); print(b) would output two different values, or can it return a copy new_b = update(b). In the second case, do you need to retain the original object? That is, after new_b = update(b), would you require id(new_b) == id(b)? Would you require that id(new_b[0]) == id(b[0]) as well? (Keeping the existing rows, and only replacing the tuples within each row?)

– chepner
Nov 19 '18 at 16:38















ee== .. is not an assignment. And ee=.. changes the variable but does not change n.

– hpaulj
Nov 19 '18 at 16:47





ee== .. is not an assignment. And ee=.. changes the variable but does not change n.

– hpaulj
Nov 19 '18 at 16:47












2 Answers
2






active

oldest

votes


















3














Tuples are immutable. You can use a list comprehension instead:



res = [[(i+5, j) for i, j in tup] for tup in b]

[[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 0.023)]]





share|improve this answer



















  • 3





    This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

    – chepner
    Nov 19 '18 at 16:32











  • @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

    – jpp
    Nov 19 '18 at 16:33








  • 1





    I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

    – chepner
    Nov 19 '18 at 16:35



















2














You cannot modify tuples, they are immutable in Python. You can however replace the existing tuples with other tuples.



b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 0.023)]]
for n in b:
for i, (ee, ww) in enumerate(n):
n[i] = (ee + 2903, ww)
print(b)


Output:



[[(2904, 0.044), (2905, 0.042)], [(2907, 0.18), (2909, 0.023)], [(2907, 0.03), (2908, 0.023)]]





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378909%2fhow-to-change-the-first-value-in-the-tuple-of-a-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Tuples are immutable. You can use a list comprehension instead:



    res = [[(i+5, j) for i, j in tup] for tup in b]

    [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 0.023)]]





    share|improve this answer



















    • 3





      This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

      – chepner
      Nov 19 '18 at 16:32











    • @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

      – jpp
      Nov 19 '18 at 16:33








    • 1





      I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

      – chepner
      Nov 19 '18 at 16:35
















    3














    Tuples are immutable. You can use a list comprehension instead:



    res = [[(i+5, j) for i, j in tup] for tup in b]

    [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 0.023)]]





    share|improve this answer



















    • 3





      This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

      – chepner
      Nov 19 '18 at 16:32











    • @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

      – jpp
      Nov 19 '18 at 16:33








    • 1





      I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

      – chepner
      Nov 19 '18 at 16:35














    3












    3








    3







    Tuples are immutable. You can use a list comprehension instead:



    res = [[(i+5, j) for i, j in tup] for tup in b]

    [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 0.023)]]





    share|improve this answer













    Tuples are immutable. You can use a list comprehension instead:



    res = [[(i+5, j) for i, j in tup] for tup in b]

    [[(6, 0.044), (7, 0.042)], [(9, 0.18), (11, 0.023)], [(9, 0.03), (10, 0.023)]]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 16:29









    jppjpp

    100k2161111




    100k2161111








    • 3





      This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

      – chepner
      Nov 19 '18 at 16:32











    • @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

      – jpp
      Nov 19 '18 at 16:33








    • 1





      I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

      – chepner
      Nov 19 '18 at 16:35














    • 3





      This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

      – chepner
      Nov 19 '18 at 16:32











    • @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

      – jpp
      Nov 19 '18 at 16:33








    • 1





      I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

      – chepner
      Nov 19 '18 at 16:35








    3




    3





    This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

    – chepner
    Nov 19 '18 at 16:32





    This builds an entirely new matrix, rather than updating the existing one. Depending on the use case, this may not be useful.

    – chepner
    Nov 19 '18 at 16:32













    @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

    – jpp
    Nov 19 '18 at 16:33







    @chepner, Sure. I think there are fundamental flaws in understanding for OP, e.g. the use of the term "matrix". I think we need more clarity to be sure a list comprehension does not suffice. Of course, time complexity is the same either way, there's no intrinsic benefit to modifying the "existing matrix" if either would work.

    – jpp
    Nov 19 '18 at 16:33






    1




    1





    I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

    – chepner
    Nov 19 '18 at 16:35





    I read the original question ("... keep the change to the original matrix b") as indicating that this might be, for example, inside a function that takes b as an argument and updates it in-place.

    – chepner
    Nov 19 '18 at 16:35













    2














    You cannot modify tuples, they are immutable in Python. You can however replace the existing tuples with other tuples.



    b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 0.023)]]
    for n in b:
    for i, (ee, ww) in enumerate(n):
    n[i] = (ee + 2903, ww)
    print(b)


    Output:



    [[(2904, 0.044), (2905, 0.042)], [(2907, 0.18), (2909, 0.023)], [(2907, 0.03), (2908, 0.023)]]





    share|improve this answer




























      2














      You cannot modify tuples, they are immutable in Python. You can however replace the existing tuples with other tuples.



      b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 0.023)]]
      for n in b:
      for i, (ee, ww) in enumerate(n):
      n[i] = (ee + 2903, ww)
      print(b)


      Output:



      [[(2904, 0.044), (2905, 0.042)], [(2907, 0.18), (2909, 0.023)], [(2907, 0.03), (2908, 0.023)]]





      share|improve this answer


























        2












        2








        2







        You cannot modify tuples, they are immutable in Python. You can however replace the existing tuples with other tuples.



        b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 0.023)]]
        for n in b:
        for i, (ee, ww) in enumerate(n):
        n[i] = (ee + 2903, ww)
        print(b)


        Output:



        [[(2904, 0.044), (2905, 0.042)], [(2907, 0.18), (2909, 0.023)], [(2907, 0.03), (2908, 0.023)]]





        share|improve this answer













        You cannot modify tuples, they are immutable in Python. You can however replace the existing tuples with other tuples.



        b = [[(1, 0.044), (2, 0.042)], [(4, 0.18), (6, 0.023)], [(4, 0.03), (5, 0.023)]]
        for n in b:
        for i, (ee, ww) in enumerate(n):
        n[i] = (ee + 2903, ww)
        print(b)


        Output:



        [[(2904, 0.044), (2905, 0.042)], [(2907, 0.18), (2909, 0.023)], [(2907, 0.03), (2908, 0.023)]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 16:31









        jdehesajdehesa

        23.8k43554




        23.8k43554






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378909%2fhow-to-change-the-first-value-in-the-tuple-of-a-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)