-4

I have list

my_list= [33,29,33,87,83,138,141,145,191,191,191]

Now i have another list

split_list = [2,4,7]

Now I need to separate my_list values by using the values in split_list as my position value of my_list,

in this link they have grouped the list in with 4 elements in each list using scala, like wise i want to use the number in split_list as grouping factor

like this

answer_list = [(33,29,33),(87,83),(138,141,145),(191,191,191)]
Deepan Raj
  • 385
  • 1
  • 5
  • 16
  • 2
    I see a requirement here, not a question. – SiHa Sep 06 '17 at 11:22
  • 1
    what did you try? – Netwave Sep 06 '17 at 11:22
  • What do you want to achieve? As in the `answer_list` I do not see a bunch of 4 or 7 .. only 2 or 3? – Jaffer Wilson Sep 06 '17 at 11:25
  • I don't have clue on how to proceed it. – Deepan Raj Sep 06 '17 at 11:26
  • using my split_list as my position i have to cut my_list till that and proceed further, like in split_list the first value is 2 , so now i need to cut 0,1,2 position of my_list a group it as a list likewise for 4 and 7 so on – Deepan Raj Sep 06 '17 at 11:27
  • @DeepanRaj So, still make your question clear for better understanding so that people will know what you are thinking. The input what you have shown and the output what you are referring doesn't make sense and people won't be able to understand you requirements. please make the question clearer, else it will be mentioned for closing. – Jaffer Wilson Sep 06 '17 at 11:28
  • @DeepanRaj dear you are still not making any sense... – Jaffer Wilson Sep 06 '17 at 11:30
  • What's happened to 138? As far as I can see it's the only dropped element. Judging by your provided slices, the third group should be `[138, 141, 145]`. – Izaak van Dongen Sep 06 '17 at 11:36
  • yes izaak .. my bad..missed that number – Deepan Raj Sep 06 '17 at 11:47

3 Answers3

3

You can do this with a list comprehension by slightly adjusting the split_list:

>>> my_list= [33,29,33,87,83,138,141,145,191,191,191]
>>> split_list = [2,4,7]
>>> split_list = [-1] + split_list + [len(my_list)]
>>> [tuple(my_list[split_list[i]+1:split_list[i+1]+1]) for i in range(len(split_list)-1)]
[(33, 29, 33), (87, 83), (138, 141, 145), (191, 191, 191)]

If the indices were instead the index of the item before which the slice occurred, you could have slightly nicer code:

>>> my_list= [33,29,33,87,83,138,141,145,191,191,191]
>>> split_list = [2,4,7]
>>> split_list = [i + 1 for i in split_list] # To change the list to [3,5,8]
>>> split_list = [None] + split_list + [None]
>>> [tuple(my_list[split_list[i]:split_list[i+1]]) for i in range(len(split_list)-1)]
[(33, 29, 33), (87, 83), (138, 141, 145), (191, 191, 191)]
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
0

Working, though not very elegant:

first = 0
answer_list = []
for last in split_list:
    answer_list.append(my_list[first:last+1])
    first = last+1
answer_list.append(my_list[first:])
Błotosmętek
  • 12,717
  • 19
  • 29
  • 2
    I don't believe this accounts for the last group (`191,191,191`). If you add `answer_list.append(my_list[first:])` after the for loop it should work. – Izaak van Dongen Sep 06 '17 at 11:39
0

You can use use list slicing:

my_list= [33,29,33,87,83,138,141,145,191,191,191]
split_list= [2,4,7]

split_list.append(len(my_list)-1)
answer_list = []
prev = 0
for i in split_list :
    answer_list.append(tuple(my_list[prev:i+1]))
    prev = i+1
print(answer_list)

output : [(33, 29, 33), (87, 83), (138, 141, 145), (191, 191, 191)]

Tushar Aggarwal
  • 827
  • 1
  • 10
  • 26