-1

I have a list that I acquired parsing a text file(delimeted by \t and \n) which I eventually have to import into a database:

list = ['1', '1', 'Thurs', '1', 'Snow', '1', 'Rockville', 'Basic', 'Medium', 
'1', 'Smith, J.', 'Junior', '5', '1', 'Chicken Noodle', 'Progresso', 
'Canned', 'Basic', '1', 'Radio', 'Morning Drive', 'Weekday', '405.18''2', 
'1', 'Thurs', '1', 'Snow', '1', 'Rockville', 'Basic', 'Medium', '1', 'Smith, 
J.', 'Junior', '5', '2', 'Chicken Noodle', 'Campbells', 'Canned', 'Basic', 
'1', 'Radio', 'Morning Drive', 'Weekday', '453.30' ..... list continues]

After each 23 items a new record starts. I though that what I need is to slice 4-5 items with a step of 23. So I am trying the following:

sliceN = slice(14-18)
listSlice=list[sliceN:-1:23]

This doesn't work. I would really appreciate any advise on solving my issue. If there are other better ways to approach it please let me know.

  • 1
    What do you mean by `slice 4-5 items with a step of 23`? Please provide sample input and the expected output – Andreas Dec 04 '18 at 09:53
  • Could you make your parser a little smarter, and have it deliver grouped tokens instead of just an ungrouped stream? (Also, you will find yourself cursing yourself for assigning to builtin names like "list" and then getting weird errors because the name "list" no longer refers to the standard builtin type - better to use something like "data_list" or "parsed_data_list", or even just "parsed_data".) And there is no reason to subgroup your 23-item lists into 4 or 5 item chunks - Python can easily handle 23 items at a time. – PaulMcG Dec 04 '18 at 13:34
  • What I meant is that I need a slice of 4 items in each 23 items of the list. So I would get lets say a slice of [13:18] in each 23 items. – James Franco Dec 04 '18 at 16:35

1 Answers1

0

I am not sure what you mean with slice 4-5 items with a step of 23, but maybe this solves your problem

code:

import numpy as np

list=np.arange(100).tolist()
print('input list')
print(list)

print('\nyour slices, if this is what you want')
for ii in range(len(list)//23):
    print(list[ii*23:ii*23+5])

result:

input list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

your slices, if this is what you want
[0, 1, 2, 3, 4]
[23, 24, 25, 26, 27]
[46, 47, 48, 49, 50]
[69, 70, 71, 72, 73]
Markus Dutschke
  • 9,341
  • 4
  • 63
  • 58
  • What I meant is that I need a slice of 4 items in each 23 items of the list. So I would get lets say a slice of [13:18] in each 23 items. So my slices out of the given list would be: ['1', 'Chicken Noodle', 'Progresso', 'Canned', 'Basic'] ['2', 'Chicken Noodle', 'Campbells', 'Canned', 'Basic'] – James Franco Dec 04 '18 at 16:37
  • so replace `[ii*23:ii*23+5]` by `[13+ii*23:13+ii*23+5]` – Markus Dutschke Dec 04 '18 at 17:45