创建子列表列表 [英] Create list of sublists
问题描述
我有50,000个整数的列表:
I have a list of 50,000 integers:
我想从这个大列表中创建一个子列表列表.
I want to create a list of sublists from this large list.
通过在列表中查找与列表的第一个值1,000,000不同的位置来创建子列表.
The sublist is created by finding within the list the position that has a difference with the first value of the list of 1,000,000.
下面是我的代码:
sub_pos_list = []
for i in range(0, len(pos_list)):
difference = pos_list[i] - pos_list[0]
if difference <= 1000000:
sub_pos_list.append(pos_list[0:i])
但是,这仅抓住了前1,000,000个差额.然后,我想从列表中删除该区域,然后重新开始.
However, this only grabs the first 1,000,000 difference. I want to then delete this region from the list and start over again.
我想创建多个差异为1,000,000的子列表,然后列出这些子列表.
I want to create multiple sublists of 1,000,000 difference and then make a list of these sublists.
任何建议如何做到这一点?
Any suggestions how to do this?
推荐答案
如果我正确理解了您想要实现的目标,则可以采用以下解决方案:
If I understood correctly what You want to achieve, this is the solution:
sub_pos_list = []
last_found_pos = 0
for i in xrange(last_found_pos, len(pos_list)):
difference = pos_list[i] - pos_list[last_found_pos]
if difference >= 1000000:
sub_pos_list.append(pos_list[last_found_pos:i + 1])
last_found_pos = i
来自: pos_list = [0,5,1000000,1000005,2000005]
它会给你: sub_pos_list == [[0,5,1000000],[1000000,1000005,2000005]]
这篇关于创建子列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!