How to combine a string list with a list of lists of integers











up vote
1
down vote

favorite












I would like to combine the following string list and integer list of lists:



lst = ['A', 
'A',
'B',
'C',
'C',
'D',
'D',
'D',....]

lst_of_lst = [[9, 10, 11, 12],
[54, 55, 56],
[72, 73, 74, 75, 76],
[1, 2, 3, 4, 5],
[98, 99, 100],
[13, 14],
[21, 22, 23],
[27, 28, 29, 30], ....]


such that a list of tuples is returned:



lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
('A', 54), ('A', 55), ('A', 56),
('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
('C', 98), ('C', 99), ('C', 100),
('D', 13), ('D', 14),
('D', 21), ('D', 22), ('D', 23),
('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]


The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst.



I have tried the following, which only works for the first element of each sublist in lst_of_lst and repeats for each string in lst:



empty_test_combo = 
for x in helix_chain_id:
for y in helix_seq_res_num_ranges:
empty_test_combo += (zip(x, y))


I have also tried:



lst_tups = 
for x in lst:
for y in lst_of_lst:
for z in y:
lst_tups.append(zip(x, [z]))


This seems the most promising option. It returns a list of tuples that combines the lst strings and lst_of_lst integer lists correctly, but only partially.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I would like to combine the following string list and integer list of lists:



    lst = ['A', 
    'A',
    'B',
    'C',
    'C',
    'D',
    'D',
    'D',....]

    lst_of_lst = [[9, 10, 11, 12],
    [54, 55, 56],
    [72, 73, 74, 75, 76],
    [1, 2, 3, 4, 5],
    [98, 99, 100],
    [13, 14],
    [21, 22, 23],
    [27, 28, 29, 30], ....]


    such that a list of tuples is returned:



    lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
    ('A', 54), ('A', 55), ('A', 56),
    ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
    ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
    ('C', 98), ('C', 99), ('C', 100),
    ('D', 13), ('D', 14),
    ('D', 21), ('D', 22), ('D', 23),
    ('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]


    The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst.



    I have tried the following, which only works for the first element of each sublist in lst_of_lst and repeats for each string in lst:



    empty_test_combo = 
    for x in helix_chain_id:
    for y in helix_seq_res_num_ranges:
    empty_test_combo += (zip(x, y))


    I have also tried:



    lst_tups = 
    for x in lst:
    for y in lst_of_lst:
    for z in y:
    lst_tups.append(zip(x, [z]))


    This seems the most promising option. It returns a list of tuples that combines the lst strings and lst_of_lst integer lists correctly, but only partially.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I would like to combine the following string list and integer list of lists:



      lst = ['A', 
      'A',
      'B',
      'C',
      'C',
      'D',
      'D',
      'D',....]

      lst_of_lst = [[9, 10, 11, 12],
      [54, 55, 56],
      [72, 73, 74, 75, 76],
      [1, 2, 3, 4, 5],
      [98, 99, 100],
      [13, 14],
      [21, 22, 23],
      [27, 28, 29, 30], ....]


      such that a list of tuples is returned:



      lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
      ('A', 54), ('A', 55), ('A', 56),
      ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
      ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
      ('C', 98), ('C', 99), ('C', 100),
      ('D', 13), ('D', 14),
      ('D', 21), ('D', 22), ('D', 23),
      ('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]


      The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst.



      I have tried the following, which only works for the first element of each sublist in lst_of_lst and repeats for each string in lst:



      empty_test_combo = 
      for x in helix_chain_id:
      for y in helix_seq_res_num_ranges:
      empty_test_combo += (zip(x, y))


      I have also tried:



      lst_tups = 
      for x in lst:
      for y in lst_of_lst:
      for z in y:
      lst_tups.append(zip(x, [z]))


      This seems the most promising option. It returns a list of tuples that combines the lst strings and lst_of_lst integer lists correctly, but only partially.










      share|improve this question















      I would like to combine the following string list and integer list of lists:



      lst = ['A', 
      'A',
      'B',
      'C',
      'C',
      'D',
      'D',
      'D',....]

      lst_of_lst = [[9, 10, 11, 12],
      [54, 55, 56],
      [72, 73, 74, 75, 76],
      [1, 2, 3, 4, 5],
      [98, 99, 100],
      [13, 14],
      [21, 22, 23],
      [27, 28, 29, 30], ....]


      such that a list of tuples is returned:



      lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
      ('A', 54), ('A', 55), ('A', 56),
      ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
      ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
      ('C', 98), ('C', 99), ('C', 100),
      ('D', 13), ('D', 14),
      ('D', 21), ('D', 22), ('D', 23),
      ('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]


      The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst.



      I have tried the following, which only works for the first element of each sublist in lst_of_lst and repeats for each string in lst:



      empty_test_combo = 
      for x in helix_chain_id:
      for y in helix_seq_res_num_ranges:
      empty_test_combo += (zip(x, y))


      I have also tried:



      lst_tups = 
      for x in lst:
      for y in lst_of_lst:
      for z in y:
      lst_tups.append(zip(x, [z]))


      This seems the most promising option. It returns a list of tuples that combines the lst strings and lst_of_lst integer lists correctly, but only partially.







      python list zip tuples






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 17:36









      Thierry Lathuille

      7,12182630




      7,12182630










      asked Nov 11 at 14:57









      Alex M

      438




      438
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          It seems that there is a misunderstanding in the way you try to use zip.



          zip(list1, list2) is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1 and the first item of list2, and so on.



          What you want to do here is to zip(lst, lst_of_lst) in order to pair each element of lst to the corresponding sublist of lst_of_lst. From each pair, you can generate the output you want.



          You can do that with a list comprehension:



          lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]

          lst_of_lst = [[9, 10, 11, 12],
          [54, 55, 56],
          [72, 73, 74, 75, 76],
          [1, 2, 3, 4, 5],
          [98, 99, 100],
          [13, 14],
          [21, 22, 23],
          [27, 28, 29, 30],]


          out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]

          print(out)
          # [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
          # ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
          # ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
          # ('D', 27), ('D', 28), ('D', 29), ('D', 30)]


          Or, written with loops, as you tried:



          out = 
          for item1, sublist in zip(lst, lst_of_lst):
          for item2 in sublist:
          out.append((item1, item2))





          share|improve this answer























          • Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
            – Alex M
            Nov 11 at 18:42











          Your Answer






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

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249949%2fhow-to-combine-a-string-list-with-a-list-of-lists-of-integers%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          It seems that there is a misunderstanding in the way you try to use zip.



          zip(list1, list2) is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1 and the first item of list2, and so on.



          What you want to do here is to zip(lst, lst_of_lst) in order to pair each element of lst to the corresponding sublist of lst_of_lst. From each pair, you can generate the output you want.



          You can do that with a list comprehension:



          lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]

          lst_of_lst = [[9, 10, 11, 12],
          [54, 55, 56],
          [72, 73, 74, 75, 76],
          [1, 2, 3, 4, 5],
          [98, 99, 100],
          [13, 14],
          [21, 22, 23],
          [27, 28, 29, 30],]


          out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]

          print(out)
          # [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
          # ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
          # ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
          # ('D', 27), ('D', 28), ('D', 29), ('D', 30)]


          Or, written with loops, as you tried:



          out = 
          for item1, sublist in zip(lst, lst_of_lst):
          for item2 in sublist:
          out.append((item1, item2))





          share|improve this answer























          • Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
            – Alex M
            Nov 11 at 18:42















          up vote
          1
          down vote



          accepted










          It seems that there is a misunderstanding in the way you try to use zip.



          zip(list1, list2) is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1 and the first item of list2, and so on.



          What you want to do here is to zip(lst, lst_of_lst) in order to pair each element of lst to the corresponding sublist of lst_of_lst. From each pair, you can generate the output you want.



          You can do that with a list comprehension:



          lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]

          lst_of_lst = [[9, 10, 11, 12],
          [54, 55, 56],
          [72, 73, 74, 75, 76],
          [1, 2, 3, 4, 5],
          [98, 99, 100],
          [13, 14],
          [21, 22, 23],
          [27, 28, 29, 30],]


          out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]

          print(out)
          # [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
          # ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
          # ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
          # ('D', 27), ('D', 28), ('D', 29), ('D', 30)]


          Or, written with loops, as you tried:



          out = 
          for item1, sublist in zip(lst, lst_of_lst):
          for item2 in sublist:
          out.append((item1, item2))





          share|improve this answer























          • Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
            – Alex M
            Nov 11 at 18:42













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          It seems that there is a misunderstanding in the way you try to use zip.



          zip(list1, list2) is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1 and the first item of list2, and so on.



          What you want to do here is to zip(lst, lst_of_lst) in order to pair each element of lst to the corresponding sublist of lst_of_lst. From each pair, you can generate the output you want.



          You can do that with a list comprehension:



          lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]

          lst_of_lst = [[9, 10, 11, 12],
          [54, 55, 56],
          [72, 73, 74, 75, 76],
          [1, 2, 3, 4, 5],
          [98, 99, 100],
          [13, 14],
          [21, 22, 23],
          [27, 28, 29, 30],]


          out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]

          print(out)
          # [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
          # ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
          # ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
          # ('D', 27), ('D', 28), ('D', 29), ('D', 30)]


          Or, written with loops, as you tried:



          out = 
          for item1, sublist in zip(lst, lst_of_lst):
          for item2 in sublist:
          out.append((item1, item2))





          share|improve this answer














          It seems that there is a misunderstanding in the way you try to use zip.



          zip(list1, list2) is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1 and the first item of list2, and so on.



          What you want to do here is to zip(lst, lst_of_lst) in order to pair each element of lst to the corresponding sublist of lst_of_lst. From each pair, you can generate the output you want.



          You can do that with a list comprehension:



          lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]

          lst_of_lst = [[9, 10, 11, 12],
          [54, 55, 56],
          [72, 73, 74, 75, 76],
          [1, 2, 3, 4, 5],
          [98, 99, 100],
          [13, 14],
          [21, 22, 23],
          [27, 28, 29, 30],]


          out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]

          print(out)
          # [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56),
          # ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
          # ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23),
          # ('D', 27), ('D', 28), ('D', 29), ('D', 30)]


          Or, written with loops, as you tried:



          out = 
          for item1, sublist in zip(lst, lst_of_lst):
          for item2 in sublist:
          out.append((item1, item2))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 17:34

























          answered Nov 11 at 17:21









          Thierry Lathuille

          7,12182630




          7,12182630












          • Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
            – Alex M
            Nov 11 at 18:42


















          • Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
            – Alex M
            Nov 11 at 18:42
















          Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
          – Alex M
          Nov 11 at 18:42




          Hi @Thierry Lathuille, that did the trick! Thanks for the explanation and the loop alternative. This made it easier to understand compared to the list comprehension.
          – Alex M
          Nov 11 at 18:42


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53249949%2fhow-to-combine-a-string-list-with-a-list-of-lists-of-integers%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

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues