How to add and extend a new row in Python list?












1















I have to reduce the density of an array, by using a for loop that traverses by steps of 100 and copy the value of my original array into a new array:



soundDataHere is a [7][22000] dim array, and I want cleanSoundData to be a [7][220] dim array



def reduceDensity(soundDataHere):
for i in range(numberOfFiles):
for j in range(0, soundDataHere[i].size-1, 100):
cleanSoundData.extend(soundDataHere[i][j])


I keep dont know how to use the append and extend function in a foor loop to recreate a new less dense array.



example: [[1,2,3,4,5],[6,7,8,9,10]] with a step = 2
should return [[1,3,5],[6,8,10]] in my new cleanSoundData array



but is only extending it like [1,3,5,6,8,10]










share|improve this question

























  • Is using the numpy module an option for you?

    – Jason Baumgartner
    Dec 29 '18 at 8:58
















1















I have to reduce the density of an array, by using a for loop that traverses by steps of 100 and copy the value of my original array into a new array:



soundDataHere is a [7][22000] dim array, and I want cleanSoundData to be a [7][220] dim array



def reduceDensity(soundDataHere):
for i in range(numberOfFiles):
for j in range(0, soundDataHere[i].size-1, 100):
cleanSoundData.extend(soundDataHere[i][j])


I keep dont know how to use the append and extend function in a foor loop to recreate a new less dense array.



example: [[1,2,3,4,5],[6,7,8,9,10]] with a step = 2
should return [[1,3,5],[6,8,10]] in my new cleanSoundData array



but is only extending it like [1,3,5,6,8,10]










share|improve this question

























  • Is using the numpy module an option for you?

    – Jason Baumgartner
    Dec 29 '18 at 8:58














1












1








1








I have to reduce the density of an array, by using a for loop that traverses by steps of 100 and copy the value of my original array into a new array:



soundDataHere is a [7][22000] dim array, and I want cleanSoundData to be a [7][220] dim array



def reduceDensity(soundDataHere):
for i in range(numberOfFiles):
for j in range(0, soundDataHere[i].size-1, 100):
cleanSoundData.extend(soundDataHere[i][j])


I keep dont know how to use the append and extend function in a foor loop to recreate a new less dense array.



example: [[1,2,3,4,5],[6,7,8,9,10]] with a step = 2
should return [[1,3,5],[6,8,10]] in my new cleanSoundData array



but is only extending it like [1,3,5,6,8,10]










share|improve this question
















I have to reduce the density of an array, by using a for loop that traverses by steps of 100 and copy the value of my original array into a new array:



soundDataHere is a [7][22000] dim array, and I want cleanSoundData to be a [7][220] dim array



def reduceDensity(soundDataHere):
for i in range(numberOfFiles):
for j in range(0, soundDataHere[i].size-1, 100):
cleanSoundData.extend(soundDataHere[i][j])


I keep dont know how to use the append and extend function in a foor loop to recreate a new less dense array.



example: [[1,2,3,4,5],[6,7,8,9,10]] with a step = 2
should return [[1,3,5],[6,8,10]] in my new cleanSoundData array



but is only extending it like [1,3,5,6,8,10]







python arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 16:02









Sergey Pugach

1,8021412




1,8021412










asked Dec 29 '18 at 8:54









Sharan NarasimhanSharan Narasimhan

218




218













  • Is using the numpy module an option for you?

    – Jason Baumgartner
    Dec 29 '18 at 8:58



















  • Is using the numpy module an option for you?

    – Jason Baumgartner
    Dec 29 '18 at 8:58

















Is using the numpy module an option for you?

– Jason Baumgartner
Dec 29 '18 at 8:58





Is using the numpy module an option for you?

– Jason Baumgartner
Dec 29 '18 at 8:58












3 Answers
3






active

oldest

votes


















0














Maybe you should try creating two different temporary list objects where you 'extend' the required elements into each one of them(that is the right and the left sublist) just after the for loop. Then 'append' those two lists instead of extending in the cleanSoundData.






share|improve this answer
























  • thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

    – Sharan Narasimhan
    Dec 29 '18 at 9:12



















0














Using Numpy and your example:



import numpy as np

l = [1,3,5,6,8,10]
l2 = np.reshape(l,[2,-1])

>>> l2
array([[ 1, 3, 5],[ 6, 8, 10]])


It looks like you're working with sound data so I would highly recommend using the numpy module as vectorizing array operations will be far faster than using a for loop with Python objects (up to 100x faster in some cases).



The Numpy module was designed specifically for these type of use cases so I would encourage you to learn how to use it.






share|improve this answer


























  • Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

    – Sharan Narasimhan
    Dec 29 '18 at 9:16











  • np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

    – Jason Baumgartner
    Dec 29 '18 at 9:21



















0














Assuming that the array soundData is a 7 X 22000 holds your data. So creating a new array cleanSoundData of size 7 x 220 can be done as follows. Even it is more generalized whether its 2 x 1000 or 1000 x 50000.



cleanSoundData = 
for i in range(len(soundData)):
cleanSoundData.append() # adding new row
for j in range(0, len(soundData[i]), 100):
cleanSoundData[i].append(soundData[i][j]) # adding data to the row


Hope this will work for you.






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%2f53968120%2fhow-to-add-and-extend-a-new-row-in-python-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Maybe you should try creating two different temporary list objects where you 'extend' the required elements into each one of them(that is the right and the left sublist) just after the for loop. Then 'append' those two lists instead of extending in the cleanSoundData.






    share|improve this answer
























    • thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

      – Sharan Narasimhan
      Dec 29 '18 at 9:12
















    0














    Maybe you should try creating two different temporary list objects where you 'extend' the required elements into each one of them(that is the right and the left sublist) just after the for loop. Then 'append' those two lists instead of extending in the cleanSoundData.






    share|improve this answer
























    • thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

      – Sharan Narasimhan
      Dec 29 '18 at 9:12














    0












    0








    0







    Maybe you should try creating two different temporary list objects where you 'extend' the required elements into each one of them(that is the right and the left sublist) just after the for loop. Then 'append' those two lists instead of extending in the cleanSoundData.






    share|improve this answer













    Maybe you should try creating two different temporary list objects where you 'extend' the required elements into each one of them(that is the right and the left sublist) just after the for loop. Then 'append' those two lists instead of extending in the cleanSoundData.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 29 '18 at 9:08









    Aditya GoyalAditya Goyal

    11




    11













    • thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

      – Sharan Narasimhan
      Dec 29 '18 at 9:12



















    • thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

      – Sharan Narasimhan
      Dec 29 '18 at 9:12

















    thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

    – Sharan Narasimhan
    Dec 29 '18 at 9:12





    thats possible but more memory hungry and slower, if theres another better method to do it that would be preferred.

    – Sharan Narasimhan
    Dec 29 '18 at 9:12













    0














    Using Numpy and your example:



    import numpy as np

    l = [1,3,5,6,8,10]
    l2 = np.reshape(l,[2,-1])

    >>> l2
    array([[ 1, 3, 5],[ 6, 8, 10]])


    It looks like you're working with sound data so I would highly recommend using the numpy module as vectorizing array operations will be far faster than using a for loop with Python objects (up to 100x faster in some cases).



    The Numpy module was designed specifically for these type of use cases so I would encourage you to learn how to use it.






    share|improve this answer


























    • Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

      – Sharan Narasimhan
      Dec 29 '18 at 9:16











    • np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

      – Jason Baumgartner
      Dec 29 '18 at 9:21
















    0














    Using Numpy and your example:



    import numpy as np

    l = [1,3,5,6,8,10]
    l2 = np.reshape(l,[2,-1])

    >>> l2
    array([[ 1, 3, 5],[ 6, 8, 10]])


    It looks like you're working with sound data so I would highly recommend using the numpy module as vectorizing array operations will be far faster than using a for loop with Python objects (up to 100x faster in some cases).



    The Numpy module was designed specifically for these type of use cases so I would encourage you to learn how to use it.






    share|improve this answer


























    • Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

      – Sharan Narasimhan
      Dec 29 '18 at 9:16











    • np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

      – Jason Baumgartner
      Dec 29 '18 at 9:21














    0












    0








    0







    Using Numpy and your example:



    import numpy as np

    l = [1,3,5,6,8,10]
    l2 = np.reshape(l,[2,-1])

    >>> l2
    array([[ 1, 3, 5],[ 6, 8, 10]])


    It looks like you're working with sound data so I would highly recommend using the numpy module as vectorizing array operations will be far faster than using a for loop with Python objects (up to 100x faster in some cases).



    The Numpy module was designed specifically for these type of use cases so I would encourage you to learn how to use it.






    share|improve this answer















    Using Numpy and your example:



    import numpy as np

    l = [1,3,5,6,8,10]
    l2 = np.reshape(l,[2,-1])

    >>> l2
    array([[ 1, 3, 5],[ 6, 8, 10]])


    It looks like you're working with sound data so I would highly recommend using the numpy module as vectorizing array operations will be far faster than using a for loop with Python objects (up to 100x faster in some cases).



    The Numpy module was designed specifically for these type of use cases so I would encourage you to learn how to use it.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 29 '18 at 9:09

























    answered Dec 29 '18 at 9:03









    Jason BaumgartnerJason Baumgartner

    292110




    292110













    • Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

      – Sharan Narasimhan
      Dec 29 '18 at 9:16











    • np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

      – Jason Baumgartner
      Dec 29 '18 at 9:21



















    • Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

      – Sharan Narasimhan
      Dec 29 '18 at 9:16











    • np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

      – Jason Baumgartner
      Dec 29 '18 at 9:21

















    Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

    – Sharan Narasimhan
    Dec 29 '18 at 9:16





    Yes okay I will consider using numpy, however its tough to use reshape all the time because all sound samples do not have the same size. But thanks I can probably work around this problem with a little bit more code

    – Sharan Narasimhan
    Dec 29 '18 at 9:16













    np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

    – Jason Baumgartner
    Dec 29 '18 at 9:21





    np.reshape(l,[2,-1]) -- the -1 here means to auto-adjust to the size so as long as the array is even sized, it will work. If you could give more info on the structure of the data, I might be able to give you some better ideas.

    – Jason Baumgartner
    Dec 29 '18 at 9:21











    0














    Assuming that the array soundData is a 7 X 22000 holds your data. So creating a new array cleanSoundData of size 7 x 220 can be done as follows. Even it is more generalized whether its 2 x 1000 or 1000 x 50000.



    cleanSoundData = 
    for i in range(len(soundData)):
    cleanSoundData.append() # adding new row
    for j in range(0, len(soundData[i]), 100):
    cleanSoundData[i].append(soundData[i][j]) # adding data to the row


    Hope this will work for you.






    share|improve this answer




























      0














      Assuming that the array soundData is a 7 X 22000 holds your data. So creating a new array cleanSoundData of size 7 x 220 can be done as follows. Even it is more generalized whether its 2 x 1000 or 1000 x 50000.



      cleanSoundData = 
      for i in range(len(soundData)):
      cleanSoundData.append() # adding new row
      for j in range(0, len(soundData[i]), 100):
      cleanSoundData[i].append(soundData[i][j]) # adding data to the row


      Hope this will work for you.






      share|improve this answer


























        0












        0








        0







        Assuming that the array soundData is a 7 X 22000 holds your data. So creating a new array cleanSoundData of size 7 x 220 can be done as follows. Even it is more generalized whether its 2 x 1000 or 1000 x 50000.



        cleanSoundData = 
        for i in range(len(soundData)):
        cleanSoundData.append() # adding new row
        for j in range(0, len(soundData[i]), 100):
        cleanSoundData[i].append(soundData[i][j]) # adding data to the row


        Hope this will work for you.






        share|improve this answer













        Assuming that the array soundData is a 7 X 22000 holds your data. So creating a new array cleanSoundData of size 7 x 220 can be done as follows. Even it is more generalized whether its 2 x 1000 or 1000 x 50000.



        cleanSoundData = 
        for i in range(len(soundData)):
        cleanSoundData.append() # adding new row
        for j in range(0, len(soundData[i]), 100):
        cleanSoundData[i].append(soundData[i][j]) # adding data to the row


        Hope this will work for you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 29 '18 at 9:26









        Anidhya BhatnagarAnidhya Bhatnagar

        46329




        46329






























            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%2f53968120%2fhow-to-add-and-extend-a-new-row-in-python-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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas