-3

I would like to create a new list with sub-lists inside whose length depends on another list, for example I have:

a = [1,3,2,5,4]
b = ['a','b','c','d','e','f','g','h','i','l','m','n','o','p','q']

and I would like to have a nested list of the form:

[['a'],
 ['b', 'c', 'd'],
 ['e', 'f'],
 ['g', 'h', 'i', 'l', 'm'],
 ['n', 'o', 'p', 'q']]
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Those weren't lists, those were tuples. I have edited your question. Please take care to ensure that your question is at least consistent. – cs95 Dec 29 '18 at 17:19
  • Now that that's out of the way, it would be great to see or hear about what you've tried? – cs95 Dec 29 '18 at 17:20
  • thanks a lot! in fact i did not manage to do anything because i am really a beginner. i have only found solutions where the length of sublists were fixed and didn't depend on other list! – Giovanni Primativo Dec 29 '18 at 17:24
  • 5
    Please understand that homework is your problem, not ours. Unless you are willing to pay us for our time or share your grade with us somehow, please at least be willing to put in that initial effort before giving up and trying to cheat your way to a solution. Any solution I were to give you now would not only not help you, but would also hinder your learning because you will believe any homework problem can be solved for you for free, without any effort from your side. You learn nothing, and everyone loses. – cs95 Dec 29 '18 at 17:26
  • The least you can do is to at least Google for similar problems to learn how they are done and then try to transfer the concepts to your problem. – Sheldore Dec 29 '18 at 17:31
  • Hello, maybe I have explained myself in a bad way: I have googled this issue all the day long but, really, i couldn't be able to find a similar issue and since i am not really handly on it I thought to ask this question. – Giovanni Primativo Dec 29 '18 at 18:10
  • This is a manual slicing, hope you can start from here: `print(b[0:1], b[1:4], b[4:6], b[6:11], b[11:16])` – iGian Dec 29 '18 at 19:21
  • Thanks, i really appreciate this! – Giovanni Primativo Dec 29 '18 at 19:23
  • You can use the marked duplicate. A list of strings can be iterated in the same way as a string. Hence the same algorithm can be used. – jpp Dec 29 '18 at 20:57

2 Answers2

2

Steps to solve this:

  1. create an empty list
  2. create a int done=0 that tells you how many things you already sliced from your data
  3. loop over all elements of your "how to cut the other list in parts"-list
    1. slice from done to done + whatever the current element of your "how to cut the other list in parts" is and append it to the empty list
  4. increment done by whatever you just sliced
  5. add the (if needed) remainder of your data

print it.


Doku:


You can read about why we do not solve your homework for you here: open letter to students with homework problems

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0
a = [1,3,2,5,4]
b = ['a','b','c','d','e','f','g','h','i','l','m','n','o','p','q']
out=[]
for number in a:
    out.append(b[:number])
    b=b[number:]
print(out)
#[['a'], ['b', 'c', 'd'], ['e', 'f'], ['g', 'h', 'i', 'l', 'm'], ['n', 'o', 'p', 'q']]

Description

The out is the final output list. The loop iterates through each element in list a (say 'number') and appends a list of that many elements from the start of list b to our output list. Then it proceeds to update list b so that those elements are removed.

Bitto
  • 7,937
  • 1
  • 16
  • 38