Repeating array with transformation












12















How can I repeat the following example sequence:



l = np.array([3,4,5,6,7])


Up to n times, doubling the values on each repetition. So for n=3:



[3, 4, 5, 6, 7, 6,  8, 10, 12, 14, 12, 16, 20, 24, 28]


Is there a simple way avoiding loops with numpy perhaps?










share|improve this question




















  • 1





    Loops would be your best bet, is there a reason you cannot use loops?

    – Zander
    Dec 20 '18 at 17:07
















12















How can I repeat the following example sequence:



l = np.array([3,4,5,6,7])


Up to n times, doubling the values on each repetition. So for n=3:



[3, 4, 5, 6, 7, 6,  8, 10, 12, 14, 12, 16, 20, 24, 28]


Is there a simple way avoiding loops with numpy perhaps?










share|improve this question




















  • 1





    Loops would be your best bet, is there a reason you cannot use loops?

    – Zander
    Dec 20 '18 at 17:07














12












12








12








How can I repeat the following example sequence:



l = np.array([3,4,5,6,7])


Up to n times, doubling the values on each repetition. So for n=3:



[3, 4, 5, 6, 7, 6,  8, 10, 12, 14, 12, 16, 20, 24, 28]


Is there a simple way avoiding loops with numpy perhaps?










share|improve this question
















How can I repeat the following example sequence:



l = np.array([3,4,5,6,7])


Up to n times, doubling the values on each repetition. So for n=3:



[3, 4, 5, 6, 7, 6,  8, 10, 12, 14, 12, 16, 20, 24, 28]


Is there a simple way avoiding loops with numpy perhaps?







python arrays list numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 '18 at 17:15









jpp

102k2165116




102k2165116










asked Dec 20 '18 at 17:04









yatuyatu

14.6k41542




14.6k41542








  • 1





    Loops would be your best bet, is there a reason you cannot use loops?

    – Zander
    Dec 20 '18 at 17:07














  • 1





    Loops would be your best bet, is there a reason you cannot use loops?

    – Zander
    Dec 20 '18 at 17:07








1




1





Loops would be your best bet, is there a reason you cannot use loops?

– Zander
Dec 20 '18 at 17:07





Loops would be your best bet, is there a reason you cannot use loops?

– Zander
Dec 20 '18 at 17:07












7 Answers
7






active

oldest

votes


















10














numpy.outer + numpy.ndarray.ravel:



>>> a = np.array([3,4,5,6,7])                                                                                          
>>> n = 3
>>> factors = 2**np.arange(n)
>>> np.outer(factors, a).ravel()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


Details:



>>> factors                                                                                                            
array([1, 2, 4])
>>> np.outer(factors, a)
array([[ 3, 4, 5, 6, 7], # 1*a
[ 6, 8, 10, 12, 14], # 2*a
[12, 16, 20, 24, 28]]) # 4*a





share|improve this answer



















  • 1





    Very nice solution, didn't know about np.outer, thanks!

    – yatu
    Dec 20 '18 at 17:22



















2














You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3 is the number of repetitions you need.



l = np.array([3,4,5,6,7])
final = np.concatenate([l*2**(i) for i in range(3)])
print (final)

array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





share|improve this answer































    1














    Here is one way:



    >>> (l * [[1], [2], [4]]).flatten()
    array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


    (Only works if you want multiples of the input.)






    share|improve this answer































      1














      You could do:



      import numpy as np

      l = np.array([3, 4, 5, 6, 7])

      rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
      print(rows.flatten())


      Output



      [ 3  4  5  6  7  6  8 10 12 14 12 16 20 24 28]





      share|improve this answer



















      • 1





        Nice one :) thx @Danielmesejo

        – yatu
        Dec 20 '18 at 17:12



















      1














      You can use np.power with np.ndarray.ravel:



      A = np.array([3,4,5,6,7])

      res = (A * np.power(2, np.arange(3))[:, None]).ravel()

      print(res)

      array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





      share|improve this answer































        0














        You can to loop over n, and concatenate a new list each time:



        l = [3,4,5,6,7]
        n = 3
        new_l =
        for i in range(n):
        new_l += [j*2**i for j in l]
        new_l = np.array(new_l)


        Output



        array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





        share|improve this answer































          0














          You can do that in pure python using list comprehensions:



          l = [3,4,5,6,7]

          n = 3
          mult = [1 * 2**i for i in range(n)]
          final = list([x*m for m in mult for x in l])





          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%2f53873120%2frepeating-array-with-transformation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            10














            numpy.outer + numpy.ndarray.ravel:



            >>> a = np.array([3,4,5,6,7])                                                                                          
            >>> n = 3
            >>> factors = 2**np.arange(n)
            >>> np.outer(factors, a).ravel()
            array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


            Details:



            >>> factors                                                                                                            
            array([1, 2, 4])
            >>> np.outer(factors, a)
            array([[ 3, 4, 5, 6, 7], # 1*a
            [ 6, 8, 10, 12, 14], # 2*a
            [12, 16, 20, 24, 28]]) # 4*a





            share|improve this answer



















            • 1





              Very nice solution, didn't know about np.outer, thanks!

              – yatu
              Dec 20 '18 at 17:22
















            10














            numpy.outer + numpy.ndarray.ravel:



            >>> a = np.array([3,4,5,6,7])                                                                                          
            >>> n = 3
            >>> factors = 2**np.arange(n)
            >>> np.outer(factors, a).ravel()
            array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


            Details:



            >>> factors                                                                                                            
            array([1, 2, 4])
            >>> np.outer(factors, a)
            array([[ 3, 4, 5, 6, 7], # 1*a
            [ 6, 8, 10, 12, 14], # 2*a
            [12, 16, 20, 24, 28]]) # 4*a





            share|improve this answer



















            • 1





              Very nice solution, didn't know about np.outer, thanks!

              – yatu
              Dec 20 '18 at 17:22














            10












            10








            10







            numpy.outer + numpy.ndarray.ravel:



            >>> a = np.array([3,4,5,6,7])                                                                                          
            >>> n = 3
            >>> factors = 2**np.arange(n)
            >>> np.outer(factors, a).ravel()
            array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


            Details:



            >>> factors                                                                                                            
            array([1, 2, 4])
            >>> np.outer(factors, a)
            array([[ 3, 4, 5, 6, 7], # 1*a
            [ 6, 8, 10, 12, 14], # 2*a
            [12, 16, 20, 24, 28]]) # 4*a





            share|improve this answer













            numpy.outer + numpy.ndarray.ravel:



            >>> a = np.array([3,4,5,6,7])                                                                                          
            >>> n = 3
            >>> factors = 2**np.arange(n)
            >>> np.outer(factors, a).ravel()
            array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


            Details:



            >>> factors                                                                                                            
            array([1, 2, 4])
            >>> np.outer(factors, a)
            array([[ 3, 4, 5, 6, 7], # 1*a
            [ 6, 8, 10, 12, 14], # 2*a
            [12, 16, 20, 24, 28]]) # 4*a






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 20 '18 at 17:14









            timgebtimgeb

            51.3k126794




            51.3k126794








            • 1





              Very nice solution, didn't know about np.outer, thanks!

              – yatu
              Dec 20 '18 at 17:22














            • 1





              Very nice solution, didn't know about np.outer, thanks!

              – yatu
              Dec 20 '18 at 17:22








            1




            1





            Very nice solution, didn't know about np.outer, thanks!

            – yatu
            Dec 20 '18 at 17:22





            Very nice solution, didn't know about np.outer, thanks!

            – yatu
            Dec 20 '18 at 17:22













            2














            You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3 is the number of repetitions you need.



            l = np.array([3,4,5,6,7])
            final = np.concatenate([l*2**(i) for i in range(3)])
            print (final)

            array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





            share|improve this answer




























              2














              You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3 is the number of repetitions you need.



              l = np.array([3,4,5,6,7])
              final = np.concatenate([l*2**(i) for i in range(3)])
              print (final)

              array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





              share|improve this answer


























                2












                2








                2







                You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3 is the number of repetitions you need.



                l = np.array([3,4,5,6,7])
                final = np.concatenate([l*2**(i) for i in range(3)])
                print (final)

                array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                share|improve this answer













                You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3 is the number of repetitions you need.



                l = np.array([3,4,5,6,7])
                final = np.concatenate([l*2**(i) for i in range(3)])
                print (final)

                array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 20 '18 at 17:10









                BazingaaBazingaa

                14.8k21230




                14.8k21230























                    1














                    Here is one way:



                    >>> (l * [[1], [2], [4]]).flatten()
                    array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


                    (Only works if you want multiples of the input.)






                    share|improve this answer




























                      1














                      Here is one way:



                      >>> (l * [[1], [2], [4]]).flatten()
                      array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


                      (Only works if you want multiples of the input.)






                      share|improve this answer


























                        1












                        1








                        1







                        Here is one way:



                        >>> (l * [[1], [2], [4]]).flatten()
                        array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


                        (Only works if you want multiples of the input.)






                        share|improve this answer













                        Here is one way:



                        >>> (l * [[1], [2], [4]]).flatten()
                        array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])


                        (Only works if you want multiples of the input.)







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 20 '18 at 17:10









                        NPENPE

                        357k67759890




                        357k67759890























                            1














                            You could do:



                            import numpy as np

                            l = np.array([3, 4, 5, 6, 7])

                            rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
                            print(rows.flatten())


                            Output



                            [ 3  4  5  6  7  6  8 10 12 14 12 16 20 24 28]





                            share|improve this answer



















                            • 1





                              Nice one :) thx @Danielmesejo

                              – yatu
                              Dec 20 '18 at 17:12
















                            1














                            You could do:



                            import numpy as np

                            l = np.array([3, 4, 5, 6, 7])

                            rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
                            print(rows.flatten())


                            Output



                            [ 3  4  5  6  7  6  8 10 12 14 12 16 20 24 28]





                            share|improve this answer



















                            • 1





                              Nice one :) thx @Danielmesejo

                              – yatu
                              Dec 20 '18 at 17:12














                            1












                            1








                            1







                            You could do:



                            import numpy as np

                            l = np.array([3, 4, 5, 6, 7])

                            rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
                            print(rows.flatten())


                            Output



                            [ 3  4  5  6  7  6  8 10 12 14 12 16 20 24 28]





                            share|improve this answer













                            You could do:



                            import numpy as np

                            l = np.array([3, 4, 5, 6, 7])

                            rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
                            print(rows.flatten())


                            Output



                            [ 3  4  5  6  7  6  8 10 12 14 12 16 20 24 28]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 20 '18 at 17:11









                            Daniel MesejoDaniel Mesejo

                            18.7k21533




                            18.7k21533








                            • 1





                              Nice one :) thx @Danielmesejo

                              – yatu
                              Dec 20 '18 at 17:12














                            • 1





                              Nice one :) thx @Danielmesejo

                              – yatu
                              Dec 20 '18 at 17:12








                            1




                            1





                            Nice one :) thx @Danielmesejo

                            – yatu
                            Dec 20 '18 at 17:12





                            Nice one :) thx @Danielmesejo

                            – yatu
                            Dec 20 '18 at 17:12











                            1














                            You can use np.power with np.ndarray.ravel:



                            A = np.array([3,4,5,6,7])

                            res = (A * np.power(2, np.arange(3))[:, None]).ravel()

                            print(res)

                            array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                            share|improve this answer




























                              1














                              You can use np.power with np.ndarray.ravel:



                              A = np.array([3,4,5,6,7])

                              res = (A * np.power(2, np.arange(3))[:, None]).ravel()

                              print(res)

                              array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                              share|improve this answer


























                                1












                                1








                                1







                                You can use np.power with np.ndarray.ravel:



                                A = np.array([3,4,5,6,7])

                                res = (A * np.power(2, np.arange(3))[:, None]).ravel()

                                print(res)

                                array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                                share|improve this answer













                                You can use np.power with np.ndarray.ravel:



                                A = np.array([3,4,5,6,7])

                                res = (A * np.power(2, np.arange(3))[:, None]).ravel()

                                print(res)

                                array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 20 '18 at 17:13









                                jppjpp

                                102k2165116




                                102k2165116























                                    0














                                    You can to loop over n, and concatenate a new list each time:



                                    l = [3,4,5,6,7]
                                    n = 3
                                    new_l =
                                    for i in range(n):
                                    new_l += [j*2**i for j in l]
                                    new_l = np.array(new_l)


                                    Output



                                    array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                                    share|improve this answer




























                                      0














                                      You can to loop over n, and concatenate a new list each time:



                                      l = [3,4,5,6,7]
                                      n = 3
                                      new_l =
                                      for i in range(n):
                                      new_l += [j*2**i for j in l]
                                      new_l = np.array(new_l)


                                      Output



                                      array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        You can to loop over n, and concatenate a new list each time:



                                        l = [3,4,5,6,7]
                                        n = 3
                                        new_l =
                                        for i in range(n):
                                        new_l += [j*2**i for j in l]
                                        new_l = np.array(new_l)


                                        Output



                                        array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])





                                        share|improve this answer













                                        You can to loop over n, and concatenate a new list each time:



                                        l = [3,4,5,6,7]
                                        n = 3
                                        new_l =
                                        for i in range(n):
                                        new_l += [j*2**i for j in l]
                                        new_l = np.array(new_l)


                                        Output



                                        array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Dec 20 '18 at 17:08









                                        TimTim

                                        1,790621




                                        1,790621























                                            0














                                            You can do that in pure python using list comprehensions:



                                            l = [3,4,5,6,7]

                                            n = 3
                                            mult = [1 * 2**i for i in range(n)]
                                            final = list([x*m for m in mult for x in l])





                                            share|improve this answer




























                                              0














                                              You can do that in pure python using list comprehensions:



                                              l = [3,4,5,6,7]

                                              n = 3
                                              mult = [1 * 2**i for i in range(n)]
                                              final = list([x*m for m in mult for x in l])





                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                You can do that in pure python using list comprehensions:



                                                l = [3,4,5,6,7]

                                                n = 3
                                                mult = [1 * 2**i for i in range(n)]
                                                final = list([x*m for m in mult for x in l])





                                                share|improve this answer













                                                You can do that in pure python using list comprehensions:



                                                l = [3,4,5,6,7]

                                                n = 3
                                                mult = [1 * 2**i for i in range(n)]
                                                final = list([x*m for m in mult for x in l])






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Dec 27 '18 at 10:08









                                                feebarsceviciusfeebarscevicius

                                                50437




                                                50437






























                                                    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%2f53873120%2frepeating-array-with-transformation%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

                                                    Bundesstraße 106

                                                    Verónica Boquete

                                                    Ida-Boy-Ed-Garten