Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用索引拆分列表_Python_List - Fatal编程技术网

Python 使用索引拆分列表

Python 使用索引拆分列表,python,list,Python,List,我正在努力将一个列表按一定的索引切碎。虽然我可以一次只做一件事,但我还没有找到一个表达式可以让我跳过逐件去做 import re # Creating list to split list = ['Leading', 'text', 'of', 'no', 'interest', '1.', 'Here', 'begins', 'section', '1', '2.', 'This', 'is', 'section', '2', '3.', 'Now', 'we', `enter cod

我正在努力将一个列表按一定的索引切碎。虽然我可以一次只做一件事,但我还没有找到一个表达式可以让我跳过逐件去做

import re

#   Creating list to split

list = ['Leading', 'text', 'of', 'no', 'interest', '1.', 'Here', 'begins', 'section', '1', '2.', 'This', 'is', 'section', '2', '3.', 'Now', 'we', `enter code here`'have', 'section', '3']


#   Identifying where sections begin and end

section_ids = [i for i, item in enumerate(list) if re.search('[0-9]+\.(?![0-9])', item)]


#   Simple creation of a new list for each section, piece by piece

section1 = list[section_ids[0]:section_ids[1]]
section2 = list[section_ids[1]:section_ids[2]]
section3 = list[section_ids[2]:]


#   Iterative creation of a new list for each claim - DOES NOT WORK

for i in range(len(section_ids)):
     if i < max(range(len(section_ids))):
          section[i] = list[section_ids[i] : list[section_ids[i + 1]]
     else:
          section[i] = list[section_ids[i] : ]
     print section[i]

#   This is what I'd like to get

#   ['1.', 'Here', 'begins', 'section', '1']
#   ['2.', 'This', 'is', 'section', '2']
#   ['3.', 'Now', 'we', 'have', 'section', '3']
重新导入
#创建要拆分的列表
列表=[“前导”、“文本”、“of”、“no”、“interest”、“1.”、“Here”、“begins”、“section”、“1”、“2.”、“This”、“is”、“section”、“2”、“3.”、“Now”、“we”和“在此处输入代码”“have”、“section”、“3”]
#确定节的开始和结束位置
部分\u id=[i代表i,如果重新搜索('[0-9]+\.(?![0-9]),则枚举(列表)中的项,项]
#为每个部分逐个创建一个新列表
section 1=列表[section_ID[0]:section_ID[1]]
section 2=列表[section_ID[1]:section_ID[2]]
第3节=列表[第2节]:]
#为每个索赔迭代创建新列表-不起作用
对于范围内的i(len(截面_ID)):
如果i
如果节ID较大,itertools版本将更高效

from itertools import izip_longest, islice
for i,j in izip_longest(section_ids, islice(section_ids, 1, None)):
    print my_list[i:j]

我能够使用以下代码生成所需的输出:

section=[]
for i,v in enumerate(section_ids+[len(list)]):
    if i==0:continue
    section.append(list[section_ids[i-1]:v])

您是否正在尝试实现以下目标:

>>> section = [] # list to hold sublists ....
>>> for index, location in enumerate(section_ids):
...     if location != section_ids[-1]: # assume its not the last one
...         section.append(list[location:section_ids[index + 1]])
...     else:
...         section.append(list[location:])
...     print section[-1]
...
['1.', 'Here', 'begins', 'section', '1']
['2.', 'This', 'is', 'section', '2']
['3.', 'Now', 'we', 'have', 'section', '3']
>>> 
或:


你的意思是让
在这里输入代码
在第3行的反勾中?你的
部分ID
是否总是从1开始,精确地增加1,并且随着你从左到右的移动而增加?隐藏
列表是个坏主意
>>> section = [] # list to hold sublists ....
>>> for index, location in enumerate(section_ids):
...     if location != section_ids[-1]: # assume its not the last one
...         section.append(list[location:section_ids[index + 1]])
...     else:
...         section.append(list[location:])
...     print section[-1]
...
['1.', 'Here', 'begins', 'section', '1']
['2.', 'This', 'is', 'section', '2']
['3.', 'Now', 'we', 'have', 'section', '3']
>>> 
>>> import re
>>> from pprint import pprint
>>> values = ['Leading', 'text', 'of', 'no', 'interest', '1.', 'Here', 'begins', 'section', '1', '2.', 'This', 'is', 'section', '2', '3.', 'Now', 'we', 'have', 'section', '3']
>>> section_ids = [i for i, item in enumerate(values) if re.search('[0-9]+\.(?![0-9])', item)] + [len(values)]
>>> section = [values[location:section_ids[index + 1]] for index, location in enumerate(section_ids) if location != section_ids[-1]]
>>> pprint(section)
[['1.', 'Here', 'begins', 'section', '1'],
 ['2.', 'This', 'is', 'section', '2'],
 ['3.', 'Now', 'we', 'have', 'section', '3']]