How to shift contents referenced by numpy array names in python











up vote
0
down vote

favorite












In python, I have numpy arrays a0, a1, and a2, each of which refer to different contents. I want to shift the relations between the reference names and the referred objects, so that a2 will now point to the content pointed by a1 before, and a1 will now point to the content pointed by a0 before. Then, I want to let a0 to point to a new content.



To be more specific, I want to do something like this:



import numpy as np
a2=np.array([1,2,3,4])
a1=np.array([10,20,30,40])
a0=np.array([8,8,8,8])
a2=a1
a1=a0
# I want to assign new values to a0[0], a0[1], .., without affecting a1.


Can I do it without copying values (e.g. by np.copy) and without memory reallocation (e.g. by del and np.empty)?










share|improve this question






















  • Why do you need to reuse the a0 array? And where do the new values come from?
    – hpaulj
    yesterday










  • What it comes down to is that you want both an unchanged a0 array, and a modified version of it. The fact that you are reusing the names ('shifting') doesn't matter.
    – hpaulj
    yesterday















up vote
0
down vote

favorite












In python, I have numpy arrays a0, a1, and a2, each of which refer to different contents. I want to shift the relations between the reference names and the referred objects, so that a2 will now point to the content pointed by a1 before, and a1 will now point to the content pointed by a0 before. Then, I want to let a0 to point to a new content.



To be more specific, I want to do something like this:



import numpy as np
a2=np.array([1,2,3,4])
a1=np.array([10,20,30,40])
a0=np.array([8,8,8,8])
a2=a1
a1=a0
# I want to assign new values to a0[0], a0[1], .., without affecting a1.


Can I do it without copying values (e.g. by np.copy) and without memory reallocation (e.g. by del and np.empty)?










share|improve this question






















  • Why do you need to reuse the a0 array? And where do the new values come from?
    – hpaulj
    yesterday










  • What it comes down to is that you want both an unchanged a0 array, and a modified version of it. The fact that you are reusing the names ('shifting') doesn't matter.
    – hpaulj
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In python, I have numpy arrays a0, a1, and a2, each of which refer to different contents. I want to shift the relations between the reference names and the referred objects, so that a2 will now point to the content pointed by a1 before, and a1 will now point to the content pointed by a0 before. Then, I want to let a0 to point to a new content.



To be more specific, I want to do something like this:



import numpy as np
a2=np.array([1,2,3,4])
a1=np.array([10,20,30,40])
a0=np.array([8,8,8,8])
a2=a1
a1=a0
# I want to assign new values to a0[0], a0[1], .., without affecting a1.


Can I do it without copying values (e.g. by np.copy) and without memory reallocation (e.g. by del and np.empty)?










share|improve this question













In python, I have numpy arrays a0, a1, and a2, each of which refer to different contents. I want to shift the relations between the reference names and the referred objects, so that a2 will now point to the content pointed by a1 before, and a1 will now point to the content pointed by a0 before. Then, I want to let a0 to point to a new content.



To be more specific, I want to do something like this:



import numpy as np
a2=np.array([1,2,3,4])
a1=np.array([10,20,30,40])
a0=np.array([8,8,8,8])
a2=a1
a1=a0
# I want to assign new values to a0[0], a0[1], .., without affecting a1.


Can I do it without copying values (e.g. by np.copy) and without memory reallocation (e.g. by del and np.empty)?







python numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









norio

1,18921123




1,18921123












  • Why do you need to reuse the a0 array? And where do the new values come from?
    – hpaulj
    yesterday










  • What it comes down to is that you want both an unchanged a0 array, and a modified version of it. The fact that you are reusing the names ('shifting') doesn't matter.
    – hpaulj
    yesterday


















  • Why do you need to reuse the a0 array? And where do the new values come from?
    – hpaulj
    yesterday










  • What it comes down to is that you want both an unchanged a0 array, and a modified version of it. The fact that you are reusing the names ('shifting') doesn't matter.
    – hpaulj
    yesterday
















Why do you need to reuse the a0 array? And where do the new values come from?
– hpaulj
yesterday




Why do you need to reuse the a0 array? And where do the new values come from?
– hpaulj
yesterday












What it comes down to is that you want both an unchanged a0 array, and a modified version of it. The fact that you are reusing the names ('shifting') doesn't matter.
– hpaulj
yesterday




What it comes down to is that you want both an unchanged a0 array, and a modified version of it. The fact that you are reusing the names ('shifting') doesn't matter.
– hpaulj
yesterday












2 Answers
2






active

oldest

votes

















up vote
2
down vote













Use tuple unpacking to exchanging values kindaof like the a,b=b,a



In [183]: a2=np.array([1,2,3,4])
...: a1=np.array([10,20,30,40])
...: a0=np.array([8,8,8,8])
...:
...:

In [184]:


In [185]: a2,a1=np.copy(a1),np.copy(a0)

In [186]: a0
Out[186]: array([8, 8, 8, 8])

In [187]: a1
Out[187]: array([8, 8, 8, 8])

In [188]: a2
Out[188]: array([10, 20, 30, 40])


You are free to point a0 where ever you want and i dont think you can create get away with changing a0 and not affecting a1 without np.copy or something else like copy.deepcopy






share|improve this answer



















  • 1




    By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
    – obachtos
    yesterday




















up vote
1
down vote













What are you asking is not possible without making a copy of a0, otherwise a0 and a1 will be pointing to the same object and changing a0 will change a1. So you should do this:



a2 = np.array([1,2,3,4])
a1 = np.array([10,20,30,40])
a0 = np.array([8,8,8,8])

a2 = a1
a1 = a0.copy()

# let's change a0
a0[0] = 9

# check
a0
Out[31]: array([9, 8, 8, 8])

a1
Out[32]: array([8, 8, 8, 8])

a2
Out[33]: array([10, 20, 30, 40])





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',
    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%2f53203678%2fhow-to-shift-contents-referenced-by-numpy-array-names-in-python%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
    2
    down vote













    Use tuple unpacking to exchanging values kindaof like the a,b=b,a



    In [183]: a2=np.array([1,2,3,4])
    ...: a1=np.array([10,20,30,40])
    ...: a0=np.array([8,8,8,8])
    ...:
    ...:

    In [184]:


    In [185]: a2,a1=np.copy(a1),np.copy(a0)

    In [186]: a0
    Out[186]: array([8, 8, 8, 8])

    In [187]: a1
    Out[187]: array([8, 8, 8, 8])

    In [188]: a2
    Out[188]: array([10, 20, 30, 40])


    You are free to point a0 where ever you want and i dont think you can create get away with changing a0 and not affecting a1 without np.copy or something else like copy.deepcopy






    share|improve this answer



















    • 1




      By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
      – obachtos
      yesterday

















    up vote
    2
    down vote













    Use tuple unpacking to exchanging values kindaof like the a,b=b,a



    In [183]: a2=np.array([1,2,3,4])
    ...: a1=np.array([10,20,30,40])
    ...: a0=np.array([8,8,8,8])
    ...:
    ...:

    In [184]:


    In [185]: a2,a1=np.copy(a1),np.copy(a0)

    In [186]: a0
    Out[186]: array([8, 8, 8, 8])

    In [187]: a1
    Out[187]: array([8, 8, 8, 8])

    In [188]: a2
    Out[188]: array([10, 20, 30, 40])


    You are free to point a0 where ever you want and i dont think you can create get away with changing a0 and not affecting a1 without np.copy or something else like copy.deepcopy






    share|improve this answer



















    • 1




      By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
      – obachtos
      yesterday















    up vote
    2
    down vote










    up vote
    2
    down vote









    Use tuple unpacking to exchanging values kindaof like the a,b=b,a



    In [183]: a2=np.array([1,2,3,4])
    ...: a1=np.array([10,20,30,40])
    ...: a0=np.array([8,8,8,8])
    ...:
    ...:

    In [184]:


    In [185]: a2,a1=np.copy(a1),np.copy(a0)

    In [186]: a0
    Out[186]: array([8, 8, 8, 8])

    In [187]: a1
    Out[187]: array([8, 8, 8, 8])

    In [188]: a2
    Out[188]: array([10, 20, 30, 40])


    You are free to point a0 where ever you want and i dont think you can create get away with changing a0 and not affecting a1 without np.copy or something else like copy.deepcopy






    share|improve this answer














    Use tuple unpacking to exchanging values kindaof like the a,b=b,a



    In [183]: a2=np.array([1,2,3,4])
    ...: a1=np.array([10,20,30,40])
    ...: a0=np.array([8,8,8,8])
    ...:
    ...:

    In [184]:


    In [185]: a2,a1=np.copy(a1),np.copy(a0)

    In [186]: a0
    Out[186]: array([8, 8, 8, 8])

    In [187]: a1
    Out[187]: array([8, 8, 8, 8])

    In [188]: a2
    Out[188]: array([10, 20, 30, 40])


    You are free to point a0 where ever you want and i dont think you can create get away with changing a0 and not affecting a1 without np.copy or something else like copy.deepcopy







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    Albin Paul

    1,180517




    1,180517








    • 1




      By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
      – obachtos
      yesterday
















    • 1




      By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
      – obachtos
      yesterday










    1




    1




    By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
    – obachtos
    yesterday






    By that a0 and a1 will point to the same contend in memory, so changing a0 will also change a1. Eventually you want to discard a2 and create a new array so I thing there is no way around copying the content of a0 you want to keep.
    – obachtos
    yesterday














    up vote
    1
    down vote













    What are you asking is not possible without making a copy of a0, otherwise a0 and a1 will be pointing to the same object and changing a0 will change a1. So you should do this:



    a2 = np.array([1,2,3,4])
    a1 = np.array([10,20,30,40])
    a0 = np.array([8,8,8,8])

    a2 = a1
    a1 = a0.copy()

    # let's change a0
    a0[0] = 9

    # check
    a0
    Out[31]: array([9, 8, 8, 8])

    a1
    Out[32]: array([8, 8, 8, 8])

    a2
    Out[33]: array([10, 20, 30, 40])





    share|improve this answer

























      up vote
      1
      down vote













      What are you asking is not possible without making a copy of a0, otherwise a0 and a1 will be pointing to the same object and changing a0 will change a1. So you should do this:



      a2 = np.array([1,2,3,4])
      a1 = np.array([10,20,30,40])
      a0 = np.array([8,8,8,8])

      a2 = a1
      a1 = a0.copy()

      # let's change a0
      a0[0] = 9

      # check
      a0
      Out[31]: array([9, 8, 8, 8])

      a1
      Out[32]: array([8, 8, 8, 8])

      a2
      Out[33]: array([10, 20, 30, 40])





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        What are you asking is not possible without making a copy of a0, otherwise a0 and a1 will be pointing to the same object and changing a0 will change a1. So you should do this:



        a2 = np.array([1,2,3,4])
        a1 = np.array([10,20,30,40])
        a0 = np.array([8,8,8,8])

        a2 = a1
        a1 = a0.copy()

        # let's change a0
        a0[0] = 9

        # check
        a0
        Out[31]: array([9, 8, 8, 8])

        a1
        Out[32]: array([8, 8, 8, 8])

        a2
        Out[33]: array([10, 20, 30, 40])





        share|improve this answer












        What are you asking is not possible without making a copy of a0, otherwise a0 and a1 will be pointing to the same object and changing a0 will change a1. So you should do this:



        a2 = np.array([1,2,3,4])
        a1 = np.array([10,20,30,40])
        a0 = np.array([8,8,8,8])

        a2 = a1
        a1 = a0.copy()

        # let's change a0
        a0[0] = 9

        # check
        a0
        Out[31]: array([9, 8, 8, 8])

        a1
        Out[32]: array([8, 8, 8, 8])

        a2
        Out[33]: array([10, 20, 30, 40])






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        AndyK

        781518




        781518






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203678%2fhow-to-shift-contents-referenced-by-numpy-array-names-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff