我有一个列表,该列表是我解析文本文件(由\ t和\ n删除)而获得的,最终我必须将其导入数据库:
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]
每23个项目之后将开始一个新记录。尽管我需要用23的步骤将4-5个项目切成薄片,所以我正在尝试以下操作:
sliceN = slice(14-18)
listSlice=list[sliceN:-1:23]
这不起作用。 我非常感谢您提供解决我问题的建议。如果还有其他更好的解决方法,请告诉我。
答案 0 :(得分:-1)
我不确定您对slice 4-5 items with a step of 23
的意思,但这也许可以解决您的问题
代码:
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])
结果:
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]