首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用索引拆分列表

使用索引拆分列表
EN

Stack Overflow用户
提问于 2012-06-27 11:19:24
回答 3查看 62关注 0票数 0

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

代码语言:javascript
复制
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']
EN

回答 3

Stack Overflow用户

发布于 2012-06-27 11:37:10

代码语言:javascript
复制
for i,j in map(None, section_ids, section_ids[1:]):
    print my_list[i:j]

如果section_ids较大,则itertools版本将更有效

代码语言:javascript
复制
from itertools import izip_longest, islice
for i,j in izip_longest(section_ids, islice(section_ids, 1, None)):
    print my_list[i:j]
票数 0
EN

Stack Overflow用户

发布于 2012-06-27 11:39:52

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

代码语言:javascript
复制
section=[]
for i,v in enumerate(section_ids+[len(list)]):
    if i==0:continue
    section.append(list[section_ids[i-1]:v])
票数 0
EN

Stack Overflow用户

发布于 2012-06-27 11:43:24

您是否正在尝试实现这样的目标:

代码语言:javascript
复制
>>> 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']
>>> 

或者:

代码语言:javascript
复制
>>> 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']]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11218928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档