使用python将列表元素转换为连续的元组

时间:2018-06-19 12:02:04

标签: python arrays list slice

a = [0, 18, 27, 43, 52, 65]

是我要从中访问更大列表切片的索引列表

[0:18],[18:27],[27:43],[43:52], [52:65], [65:]

如何做到?

我尝试过但格式不正确

slicing = [index for index in (a[:-1])]

但这给了我[0, 18, 27, 43, 52]

2 个答案:

答案 0 :(得分:2)

使用zip

a = [0, 18, 27, 43, 52, 65]    
res = [bigger_list[x:y] for x, y in zip(a[:-1], a[1:])]

最后,将其扩展:

res.extend(bigger_list[a[-1]:])

或单行执行:

res = [bigger_list[x:y] for x, y in zip(a[:-1], a[1:])] + bigger_list[a[-1]:]

答案 1 :(得分:0)

经过一些调整和搜索后,我找到了答案:

res = [bigger_list[i: a[ind+1]] for ind,i in enumerate(a[:-1])]

这也给了我slicing of bigger_list