Adding spaces to string based on listHow can I assign a value in a list comprehension?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonHow do I iterate over the words of a string?What is the difference between Python's list methods append and extend?How do you split a list into evenly sized chunks?Convert bytes to a string?How do I split a string on a delimiter in Bash?How to make a flat list out of list of listsHow to clone or copy a list?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

Employer demanding to see degree after poor code review

Looking for a soft substance that doesn't dissolve underwater

Does the unit of measure matter when you are solving for the diameter of a circumference?

What is a Centaur Thief's climbing speed?

Were pens caps holes designed to prevent death by suffocation if swallowed?

Installed Electric Tankless Water Heater - Internet loss when active

Is CD audio quality good enough?

Could a 19.25mm revolver actually exist?

At what point in European history could a government build a printing press given a basic description?

Employer asking for online access to bank account - Is this a scam?

How to know if a folder is a symbolic link?

Should one buy new hardware after a system compromise?

Is the Starlink array really visible from Earth?

What are these arcade games in Ghostbusters 1984?

Is real public IP Address hidden when using a system wide proxy in Windows 10?

The art of clickbait captions

Pirate democracy at its finest

Use backslash or single-quotes for field separation

Reduction from Exact Cover to Fixed Exact Cover

Is it rude to call a professor by their last name with no prefix in a non-academic setting?

pic versus macro in TikZ

How to illustrate the Mean Value theorem?

Defining the standard model of PA so that a space alien could understand

What is the largest (size) solid object ever dropped from an airplane to impact the ground in freefall?



Adding spaces to string based on list


How can I assign a value in a list comprehension?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonHow do I iterate over the words of a string?What is the difference between Python's list methods append and extend?How do you split a list into evenly sized chunks?Convert bytes to a string?How do I split a string on a delimiter in Bash?How to make a flat list out of list of listsHow to clone or copy a list?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6















I have the string and array.
String have the same amount of alphabetic character like array.
I need to split s to list that have equal lenght of each element like arr.



s = 'Pythonisanprogramminglanguage'

arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']


expected == ['Python', 'is', 'an', 'programming', 'language']









share|improve this question









New contributor



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














  • 3





    What have you tried so far?

    – Klaus D.
    8 hours ago

















6















I have the string and array.
String have the same amount of alphabetic character like array.
I need to split s to list that have equal lenght of each element like arr.



s = 'Pythonisanprogramminglanguage'

arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']


expected == ['Python', 'is', 'an', 'programming', 'language']









share|improve this question









New contributor



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














  • 3





    What have you tried so far?

    – Klaus D.
    8 hours ago













6












6








6








I have the string and array.
String have the same amount of alphabetic character like array.
I need to split s to list that have equal lenght of each element like arr.



s = 'Pythonisanprogramminglanguage'

arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']


expected == ['Python', 'is', 'an', 'programming', 'language']









share|improve this question









New contributor



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











I have the string and array.
String have the same amount of alphabetic character like array.
I need to split s to list that have equal lenght of each element like arr.



s = 'Pythonisanprogramminglanguage'

arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']


expected == ['Python', 'is', 'an', 'programming', 'language']






python list split






share|improve this question









New contributor



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










share|improve this question









New contributor



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








share|improve this question




share|improve this question








edited 5 hours ago









martineau

71.9k1093191




71.9k1093191






New contributor



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








asked 8 hours ago









VNGuVNGu

371




371




New contributor



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




New contributor




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









  • 3





    What have you tried so far?

    – Klaus D.
    8 hours ago












  • 3





    What have you tried so far?

    – Klaus D.
    8 hours ago







3




3





What have you tried so far?

– Klaus D.
8 hours ago





What have you tried so far?

– Klaus D.
8 hours ago












11 Answers
11






active

oldest

votes


















7














It is much cleaner to use iter with next:



s = 'Pythonisanprogramminglanguage'
arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
new_s = iter(s)
result = [''.join(next(new_s) for _ in i) for i in arr]


Output:



['Python', 'is', 'an', 'programming', 'language']





share|improve this answer






























    3














    One way would be to do this:



    s = 'Pythonisanprogramminglanguage'

    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

    expected = []
    i = 0
    for word in arr:
    expected.append(s[i:i+len(word)])
    i+= len(word)

    print(expected)





    share|improve this answer























    • Memo to self for next time: use shorter variables to be faster than Simon ;)

      – sekky
      8 hours ago






    • 1





      @sekky No effort is wasted, we are just training future AI to learn code equivalence.

      – Simon
      8 hours ago


















    3














    Using a simple for loop this can be done as follows:



    s = 'Pythonisanprogramminglanguage'

    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

    start_index = 0
    expected = list()
    for a in arr:
    expected.append(s[start_index:start_index+len(a)])
    start_index += len(a)

    print(expected)





    share|improve this answer






























      3














      In the future, an alternative approach will be to use an assignment expression (new in Python 3.8):



      s = 'Pythonisanprogramminglanguage' 
      arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

      i = 0
      expected = [s[i:(i := i+len(word))] for word in arr]





      share|improve this answer























      • Ah, walrus to the rescue !

        – Arkistarvh Kltzuonstev
        7 hours ago


















      2














      You can use itertools.accumulate to get the positions where you want to split the string:



      >>> s = 'Pythonisanprogramminglanguage'
      >>> arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
      >>> import itertools
      >>> L = list(itertools.accumulate(map(len, arr)))
      >>> L
      [6, 8, 10, 21, 29]


      Now if you zip the list with itself, you get the intervals:



      >>> list(zip([0]+L, L))
      [(0, 6), (6, 8), (8, 10), (10, 21), (21, 29)]


      And you just have to use the intervals to split the string:



      >>> [s[i:j] for i,j in zip([0]+L, L)]
      ['Python', 'is', 'an', 'programming', 'language']





      share|improve this answer






























        1














        Create a simple loop and use the length of the words as your index:



        s = 'Pythonisanprogramminglanguage' 
        arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

        ctr = 0
        words = []
        for x in arr:
        words.append(s[ctr:len(x) + ctr])
        ctr += len(x)

        print(words)

        # ['Python', 'is', 'an', 'programming', 'language']





        share|improve this answer






























          1














          The itertools module has a function named accumulate() (added in Py 3.2) to help make this relatively easy:



          from itertools import accumulate # added in Py 3.2


          s = 'Pythonisanprogramminglanguage'
          arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

          cuts = tuple(accumulate(len(item) for item in arr))
          words = [s[i:j] for i, j in zip((0,)+cuts, cuts)]
          print(words) # -> ['Python', 'is', 'an', 'programming', 'language']





          share|improve this answer
































            0














            Here is another approach :



            import numpy as np
            ar = [0]+list(map(len, arr))
            ar = list(np.cumsum(ar))
            output_ = [s[i:ar[ar.index(i)+1]] for i in ar[:-1]]


            Output :



            ['Python', 'is', 'an', 'programming', 'language']





            share|improve this answer






























              0














              One more way



              a,l = 0,[]
              for i in map(len,arr):
              l.append(s[a:a+i])
              a+=i
              print (l)
              #['Python', 'is', 'an', 'programming', 'language']





              share|improve this answer






























                0














                Props to the answer using iter. The accumulate answers are my favorite. Here is another accumulate answer using map instead of a list comprehension



                import itertools

                s = 'Pythonisanprogramminglanguage'
                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                ticks = itertools.accumulate(map(len, arr[0:]))
                words = list(map(lambda i, x: s[i:len(x) + i], (0,) + tuple(ticks), arr))


                Output:



                ['Python', 'is', 'an', 'programming', 'language']





                share|improve this answer






























                  0














                  You could collect slices off the front of s.



                  output = []

                  for word in arr:
                  i = len(word)
                  chunk, s = s[:i], s[i:]
                  output.append(chunk)

                  print(output) # -> ['Python', 'is', 'an', 'programming', 'language']





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



                    );






                    VNGu is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56305553%2fadding-spaces-to-string-based-on-list%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown

























                    11 Answers
                    11






                    active

                    oldest

                    votes








                    11 Answers
                    11






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    7














                    It is much cleaner to use iter with next:



                    s = 'Pythonisanprogramminglanguage'
                    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                    new_s = iter(s)
                    result = [''.join(next(new_s) for _ in i) for i in arr]


                    Output:



                    ['Python', 'is', 'an', 'programming', 'language']





                    share|improve this answer



























                      7














                      It is much cleaner to use iter with next:



                      s = 'Pythonisanprogramminglanguage'
                      arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                      new_s = iter(s)
                      result = [''.join(next(new_s) for _ in i) for i in arr]


                      Output:



                      ['Python', 'is', 'an', 'programming', 'language']





                      share|improve this answer

























                        7












                        7








                        7







                        It is much cleaner to use iter with next:



                        s = 'Pythonisanprogramminglanguage'
                        arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                        new_s = iter(s)
                        result = [''.join(next(new_s) for _ in i) for i in arr]


                        Output:



                        ['Python', 'is', 'an', 'programming', 'language']





                        share|improve this answer













                        It is much cleaner to use iter with next:



                        s = 'Pythonisanprogramminglanguage'
                        arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                        new_s = iter(s)
                        result = [''.join(next(new_s) for _ in i) for i in arr]


                        Output:



                        ['Python', 'is', 'an', 'programming', 'language']






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 7 hours ago









                        Ajax1234Ajax1234

                        44.4k42957




                        44.4k42957























                            3














                            One way would be to do this:



                            s = 'Pythonisanprogramminglanguage'

                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                            expected = []
                            i = 0
                            for word in arr:
                            expected.append(s[i:i+len(word)])
                            i+= len(word)

                            print(expected)





                            share|improve this answer























                            • Memo to self for next time: use shorter variables to be faster than Simon ;)

                              – sekky
                              8 hours ago






                            • 1





                              @sekky No effort is wasted, we are just training future AI to learn code equivalence.

                              – Simon
                              8 hours ago















                            3














                            One way would be to do this:



                            s = 'Pythonisanprogramminglanguage'

                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                            expected = []
                            i = 0
                            for word in arr:
                            expected.append(s[i:i+len(word)])
                            i+= len(word)

                            print(expected)





                            share|improve this answer























                            • Memo to self for next time: use shorter variables to be faster than Simon ;)

                              – sekky
                              8 hours ago






                            • 1





                              @sekky No effort is wasted, we are just training future AI to learn code equivalence.

                              – Simon
                              8 hours ago













                            3












                            3








                            3







                            One way would be to do this:



                            s = 'Pythonisanprogramminglanguage'

                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                            expected = []
                            i = 0
                            for word in arr:
                            expected.append(s[i:i+len(word)])
                            i+= len(word)

                            print(expected)





                            share|improve this answer













                            One way would be to do this:



                            s = 'Pythonisanprogramminglanguage'

                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                            expected = []
                            i = 0
                            for word in arr:
                            expected.append(s[i:i+len(word)])
                            i+= len(word)

                            print(expected)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 8 hours ago









                            SimonSimon

                            1,99253055




                            1,99253055












                            • Memo to self for next time: use shorter variables to be faster than Simon ;)

                              – sekky
                              8 hours ago






                            • 1





                              @sekky No effort is wasted, we are just training future AI to learn code equivalence.

                              – Simon
                              8 hours ago

















                            • Memo to self for next time: use shorter variables to be faster than Simon ;)

                              – sekky
                              8 hours ago






                            • 1





                              @sekky No effort is wasted, we are just training future AI to learn code equivalence.

                              – Simon
                              8 hours ago
















                            Memo to self for next time: use shorter variables to be faster than Simon ;)

                            – sekky
                            8 hours ago





                            Memo to self for next time: use shorter variables to be faster than Simon ;)

                            – sekky
                            8 hours ago




                            1




                            1





                            @sekky No effort is wasted, we are just training future AI to learn code equivalence.

                            – Simon
                            8 hours ago





                            @sekky No effort is wasted, we are just training future AI to learn code equivalence.

                            – Simon
                            8 hours ago











                            3














                            Using a simple for loop this can be done as follows:



                            s = 'Pythonisanprogramminglanguage'

                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                            start_index = 0
                            expected = list()
                            for a in arr:
                            expected.append(s[start_index:start_index+len(a)])
                            start_index += len(a)

                            print(expected)





                            share|improve this answer



























                              3














                              Using a simple for loop this can be done as follows:



                              s = 'Pythonisanprogramminglanguage'

                              arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                              start_index = 0
                              expected = list()
                              for a in arr:
                              expected.append(s[start_index:start_index+len(a)])
                              start_index += len(a)

                              print(expected)





                              share|improve this answer

























                                3












                                3








                                3







                                Using a simple for loop this can be done as follows:



                                s = 'Pythonisanprogramminglanguage'

                                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                start_index = 0
                                expected = list()
                                for a in arr:
                                expected.append(s[start_index:start_index+len(a)])
                                start_index += len(a)

                                print(expected)





                                share|improve this answer













                                Using a simple for loop this can be done as follows:



                                s = 'Pythonisanprogramminglanguage'

                                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                start_index = 0
                                expected = list()
                                for a in arr:
                                expected.append(s[start_index:start_index+len(a)])
                                start_index += len(a)

                                print(expected)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 8 hours ago









                                sekkysekky

                                636612




                                636612





















                                    3














                                    In the future, an alternative approach will be to use an assignment expression (new in Python 3.8):



                                    s = 'Pythonisanprogramminglanguage' 
                                    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                    i = 0
                                    expected = [s[i:(i := i+len(word))] for word in arr]





                                    share|improve this answer























                                    • Ah, walrus to the rescue !

                                      – Arkistarvh Kltzuonstev
                                      7 hours ago















                                    3














                                    In the future, an alternative approach will be to use an assignment expression (new in Python 3.8):



                                    s = 'Pythonisanprogramminglanguage' 
                                    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                    i = 0
                                    expected = [s[i:(i := i+len(word))] for word in arr]





                                    share|improve this answer























                                    • Ah, walrus to the rescue !

                                      – Arkistarvh Kltzuonstev
                                      7 hours ago













                                    3












                                    3








                                    3







                                    In the future, an alternative approach will be to use an assignment expression (new in Python 3.8):



                                    s = 'Pythonisanprogramminglanguage' 
                                    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                    i = 0
                                    expected = [s[i:(i := i+len(word))] for word in arr]





                                    share|improve this answer













                                    In the future, an alternative approach will be to use an assignment expression (new in Python 3.8):



                                    s = 'Pythonisanprogramminglanguage' 
                                    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                    i = 0
                                    expected = [s[i:(i := i+len(word))] for word in arr]






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 7 hours ago









                                    user200783user200783

                                    6,46095292




                                    6,46095292












                                    • Ah, walrus to the rescue !

                                      – Arkistarvh Kltzuonstev
                                      7 hours ago

















                                    • Ah, walrus to the rescue !

                                      – Arkistarvh Kltzuonstev
                                      7 hours ago
















                                    Ah, walrus to the rescue !

                                    – Arkistarvh Kltzuonstev
                                    7 hours ago





                                    Ah, walrus to the rescue !

                                    – Arkistarvh Kltzuonstev
                                    7 hours ago











                                    2














                                    You can use itertools.accumulate to get the positions where you want to split the string:



                                    >>> s = 'Pythonisanprogramminglanguage'
                                    >>> arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                    >>> import itertools
                                    >>> L = list(itertools.accumulate(map(len, arr)))
                                    >>> L
                                    [6, 8, 10, 21, 29]


                                    Now if you zip the list with itself, you get the intervals:



                                    >>> list(zip([0]+L, L))
                                    [(0, 6), (6, 8), (8, 10), (10, 21), (21, 29)]


                                    And you just have to use the intervals to split the string:



                                    >>> [s[i:j] for i,j in zip([0]+L, L)]
                                    ['Python', 'is', 'an', 'programming', 'language']





                                    share|improve this answer



























                                      2














                                      You can use itertools.accumulate to get the positions where you want to split the string:



                                      >>> s = 'Pythonisanprogramminglanguage'
                                      >>> arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                      >>> import itertools
                                      >>> L = list(itertools.accumulate(map(len, arr)))
                                      >>> L
                                      [6, 8, 10, 21, 29]


                                      Now if you zip the list with itself, you get the intervals:



                                      >>> list(zip([0]+L, L))
                                      [(0, 6), (6, 8), (8, 10), (10, 21), (21, 29)]


                                      And you just have to use the intervals to split the string:



                                      >>> [s[i:j] for i,j in zip([0]+L, L)]
                                      ['Python', 'is', 'an', 'programming', 'language']





                                      share|improve this answer

























                                        2












                                        2








                                        2







                                        You can use itertools.accumulate to get the positions where you want to split the string:



                                        >>> s = 'Pythonisanprogramminglanguage'
                                        >>> arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                        >>> import itertools
                                        >>> L = list(itertools.accumulate(map(len, arr)))
                                        >>> L
                                        [6, 8, 10, 21, 29]


                                        Now if you zip the list with itself, you get the intervals:



                                        >>> list(zip([0]+L, L))
                                        [(0, 6), (6, 8), (8, 10), (10, 21), (21, 29)]


                                        And you just have to use the intervals to split the string:



                                        >>> [s[i:j] for i,j in zip([0]+L, L)]
                                        ['Python', 'is', 'an', 'programming', 'language']





                                        share|improve this answer













                                        You can use itertools.accumulate to get the positions where you want to split the string:



                                        >>> s = 'Pythonisanprogramminglanguage'
                                        >>> arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                        >>> import itertools
                                        >>> L = list(itertools.accumulate(map(len, arr)))
                                        >>> L
                                        [6, 8, 10, 21, 29]


                                        Now if you zip the list with itself, you get the intervals:



                                        >>> list(zip([0]+L, L))
                                        [(0, 6), (6, 8), (8, 10), (10, 21), (21, 29)]


                                        And you just have to use the intervals to split the string:



                                        >>> [s[i:j] for i,j in zip([0]+L, L)]
                                        ['Python', 'is', 'an', 'programming', 'language']






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 6 hours ago









                                        jferardjferard

                                        2,8001415




                                        2,8001415





















                                            1














                                            Create a simple loop and use the length of the words as your index:



                                            s = 'Pythonisanprogramminglanguage' 
                                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                            ctr = 0
                                            words = []
                                            for x in arr:
                                            words.append(s[ctr:len(x) + ctr])
                                            ctr += len(x)

                                            print(words)

                                            # ['Python', 'is', 'an', 'programming', 'language']





                                            share|improve this answer



























                                              1














                                              Create a simple loop and use the length of the words as your index:



                                              s = 'Pythonisanprogramminglanguage' 
                                              arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                              ctr = 0
                                              words = []
                                              for x in arr:
                                              words.append(s[ctr:len(x) + ctr])
                                              ctr += len(x)

                                              print(words)

                                              # ['Python', 'is', 'an', 'programming', 'language']





                                              share|improve this answer

























                                                1












                                                1








                                                1







                                                Create a simple loop and use the length of the words as your index:



                                                s = 'Pythonisanprogramminglanguage' 
                                                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                                ctr = 0
                                                words = []
                                                for x in arr:
                                                words.append(s[ctr:len(x) + ctr])
                                                ctr += len(x)

                                                print(words)

                                                # ['Python', 'is', 'an', 'programming', 'language']





                                                share|improve this answer













                                                Create a simple loop and use the length of the words as your index:



                                                s = 'Pythonisanprogramminglanguage' 
                                                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                                ctr = 0
                                                words = []
                                                for x in arr:
                                                words.append(s[ctr:len(x) + ctr])
                                                ctr += len(x)

                                                print(words)

                                                # ['Python', 'is', 'an', 'programming', 'language']






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 8 hours ago









                                                AK47AK47

                                                5,10421940




                                                5,10421940





















                                                    1














                                                    The itertools module has a function named accumulate() (added in Py 3.2) to help make this relatively easy:



                                                    from itertools import accumulate # added in Py 3.2


                                                    s = 'Pythonisanprogramminglanguage'
                                                    arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                                    cuts = tuple(accumulate(len(item) for item in arr))
                                                    words = [s[i:j] for i, j in zip((0,)+cuts, cuts)]
                                                    print(words) # -> ['Python', 'is', 'an', 'programming', 'language']





                                                    share|improve this answer





























                                                      1














                                                      The itertools module has a function named accumulate() (added in Py 3.2) to help make this relatively easy:



                                                      from itertools import accumulate # added in Py 3.2


                                                      s = 'Pythonisanprogramminglanguage'
                                                      arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                                      cuts = tuple(accumulate(len(item) for item in arr))
                                                      words = [s[i:j] for i, j in zip((0,)+cuts, cuts)]
                                                      print(words) # -> ['Python', 'is', 'an', 'programming', 'language']





                                                      share|improve this answer



























                                                        1












                                                        1








                                                        1







                                                        The itertools module has a function named accumulate() (added in Py 3.2) to help make this relatively easy:



                                                        from itertools import accumulate # added in Py 3.2


                                                        s = 'Pythonisanprogramminglanguage'
                                                        arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                                        cuts = tuple(accumulate(len(item) for item in arr))
                                                        words = [s[i:j] for i, j in zip((0,)+cuts, cuts)]
                                                        print(words) # -> ['Python', 'is', 'an', 'programming', 'language']





                                                        share|improve this answer















                                                        The itertools module has a function named accumulate() (added in Py 3.2) to help make this relatively easy:



                                                        from itertools import accumulate # added in Py 3.2


                                                        s = 'Pythonisanprogramminglanguage'
                                                        arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']

                                                        cuts = tuple(accumulate(len(item) for item in arr))
                                                        words = [s[i:j] for i, j in zip((0,)+cuts, cuts)]
                                                        print(words) # -> ['Python', 'is', 'an', 'programming', 'language']






                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited 4 hours ago









                                                        wjandrea

                                                        1,8191332




                                                        1,8191332










                                                        answered 5 hours ago









                                                        martineaumartineau

                                                        71.9k1093191




                                                        71.9k1093191





















                                                            0














                                                            Here is another approach :



                                                            import numpy as np
                                                            ar = [0]+list(map(len, arr))
                                                            ar = list(np.cumsum(ar))
                                                            output_ = [s[i:ar[ar.index(i)+1]] for i in ar[:-1]]


                                                            Output :



                                                            ['Python', 'is', 'an', 'programming', 'language']





                                                            share|improve this answer



























                                                              0














                                                              Here is another approach :



                                                              import numpy as np
                                                              ar = [0]+list(map(len, arr))
                                                              ar = list(np.cumsum(ar))
                                                              output_ = [s[i:ar[ar.index(i)+1]] for i in ar[:-1]]


                                                              Output :



                                                              ['Python', 'is', 'an', 'programming', 'language']





                                                              share|improve this answer

























                                                                0












                                                                0








                                                                0







                                                                Here is another approach :



                                                                import numpy as np
                                                                ar = [0]+list(map(len, arr))
                                                                ar = list(np.cumsum(ar))
                                                                output_ = [s[i:ar[ar.index(i)+1]] for i in ar[:-1]]


                                                                Output :



                                                                ['Python', 'is', 'an', 'programming', 'language']





                                                                share|improve this answer













                                                                Here is another approach :



                                                                import numpy as np
                                                                ar = [0]+list(map(len, arr))
                                                                ar = list(np.cumsum(ar))
                                                                output_ = [s[i:ar[ar.index(i)+1]] for i in ar[:-1]]


                                                                Output :



                                                                ['Python', 'is', 'an', 'programming', 'language']






                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered 7 hours ago









                                                                Arkistarvh KltzuonstevArkistarvh Kltzuonstev

                                                                2,67711128




                                                                2,67711128





















                                                                    0














                                                                    One more way



                                                                    a,l = 0,[]
                                                                    for i in map(len,arr):
                                                                    l.append(s[a:a+i])
                                                                    a+=i
                                                                    print (l)
                                                                    #['Python', 'is', 'an', 'programming', 'language']





                                                                    share|improve this answer



























                                                                      0














                                                                      One more way



                                                                      a,l = 0,[]
                                                                      for i in map(len,arr):
                                                                      l.append(s[a:a+i])
                                                                      a+=i
                                                                      print (l)
                                                                      #['Python', 'is', 'an', 'programming', 'language']





                                                                      share|improve this answer

























                                                                        0












                                                                        0








                                                                        0







                                                                        One more way



                                                                        a,l = 0,[]
                                                                        for i in map(len,arr):
                                                                        l.append(s[a:a+i])
                                                                        a+=i
                                                                        print (l)
                                                                        #['Python', 'is', 'an', 'programming', 'language']





                                                                        share|improve this answer













                                                                        One more way



                                                                        a,l = 0,[]
                                                                        for i in map(len,arr):
                                                                        l.append(s[a:a+i])
                                                                        a+=i
                                                                        print (l)
                                                                        #['Python', 'is', 'an', 'programming', 'language']






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered 7 hours ago









                                                                        TranshumanTranshuman

                                                                        2,8811412




                                                                        2,8811412





















                                                                            0














                                                                            Props to the answer using iter. The accumulate answers are my favorite. Here is another accumulate answer using map instead of a list comprehension



                                                                            import itertools

                                                                            s = 'Pythonisanprogramminglanguage'
                                                                            arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                                                            ticks = itertools.accumulate(map(len, arr[0:]))
                                                                            words = list(map(lambda i, x: s[i:len(x) + i], (0,) + tuple(ticks), arr))


                                                                            Output:



                                                                            ['Python', 'is', 'an', 'programming', 'language']





                                                                            share|improve this answer



























                                                                              0














                                                                              Props to the answer using iter. The accumulate answers are my favorite. Here is another accumulate answer using map instead of a list comprehension



                                                                              import itertools

                                                                              s = 'Pythonisanprogramminglanguage'
                                                                              arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                                                              ticks = itertools.accumulate(map(len, arr[0:]))
                                                                              words = list(map(lambda i, x: s[i:len(x) + i], (0,) + tuple(ticks), arr))


                                                                              Output:



                                                                              ['Python', 'is', 'an', 'programming', 'language']





                                                                              share|improve this answer

























                                                                                0












                                                                                0








                                                                                0







                                                                                Props to the answer using iter. The accumulate answers are my favorite. Here is another accumulate answer using map instead of a list comprehension



                                                                                import itertools

                                                                                s = 'Pythonisanprogramminglanguage'
                                                                                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                                                                ticks = itertools.accumulate(map(len, arr[0:]))
                                                                                words = list(map(lambda i, x: s[i:len(x) + i], (0,) + tuple(ticks), arr))


                                                                                Output:



                                                                                ['Python', 'is', 'an', 'programming', 'language']





                                                                                share|improve this answer













                                                                                Props to the answer using iter. The accumulate answers are my favorite. Here is another accumulate answer using map instead of a list comprehension



                                                                                import itertools

                                                                                s = 'Pythonisanprogramminglanguage'
                                                                                arr = ['lkjhgf', 'zx', 'qw', 'ertyuiopakk', 'foacdhlc']
                                                                                ticks = itertools.accumulate(map(len, arr[0:]))
                                                                                words = list(map(lambda i, x: s[i:len(x) + i], (0,) + tuple(ticks), arr))


                                                                                Output:



                                                                                ['Python', 'is', 'an', 'programming', 'language']






                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered 5 hours ago









                                                                                Buckeye14GuyBuckeye14Guy

                                                                                446




                                                                                446





















                                                                                    0














                                                                                    You could collect slices off the front of s.



                                                                                    output = []

                                                                                    for word in arr:
                                                                                    i = len(word)
                                                                                    chunk, s = s[:i], s[i:]
                                                                                    output.append(chunk)

                                                                                    print(output) # -> ['Python', 'is', 'an', 'programming', 'language']





                                                                                    share|improve this answer



























                                                                                      0














                                                                                      You could collect slices off the front of s.



                                                                                      output = []

                                                                                      for word in arr:
                                                                                      i = len(word)
                                                                                      chunk, s = s[:i], s[i:]
                                                                                      output.append(chunk)

                                                                                      print(output) # -> ['Python', 'is', 'an', 'programming', 'language']





                                                                                      share|improve this answer

























                                                                                        0












                                                                                        0








                                                                                        0







                                                                                        You could collect slices off the front of s.



                                                                                        output = []

                                                                                        for word in arr:
                                                                                        i = len(word)
                                                                                        chunk, s = s[:i], s[i:]
                                                                                        output.append(chunk)

                                                                                        print(output) # -> ['Python', 'is', 'an', 'programming', 'language']





                                                                                        share|improve this answer













                                                                                        You could collect slices off the front of s.



                                                                                        output = []

                                                                                        for word in arr:
                                                                                        i = len(word)
                                                                                        chunk, s = s[:i], s[i:]
                                                                                        output.append(chunk)

                                                                                        print(output) # -> ['Python', 'is', 'an', 'programming', 'language']






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered 4 hours ago









                                                                                        wjandreawjandrea

                                                                                        1,8191332




                                                                                        1,8191332




















                                                                                            VNGu is a new contributor. Be nice, and check out our Code of Conduct.









                                                                                            draft saved

                                                                                            draft discarded


















                                                                                            VNGu is a new contributor. Be nice, and check out our Code of Conduct.












                                                                                            VNGu is a new contributor. Be nice, and check out our Code of Conduct.











                                                                                            VNGu is a new contributor. Be nice, and check out our Code of Conduct.














                                                                                            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%2f56305553%2fadding-spaces-to-string-based-on-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

                                                                                            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                                                                            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                                                                            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480