繁体   English   中英

我如何让python接受元素0和1(如果存在)而取元素0(如果元素1不存在)呢?

[英]How do I get python to take element 0 and 1 if they exist and take element 0 if element 1 does not exist?

尝试运行代码时出现“索引错误:列表索引超出范围错误”。 本质上,我想确定在文档中引用“项目1A”的所有时间,并保留该部分。 不幸的是,有些文档有目录(TOC),有些则没有。 如果有一个目录,那么我想要的元素就是元素1,当没有目录时,我想要元素0。我认为最简单的就是只保留元素0和1,因为拥有一个元素不会有伤害从那里的TOC线。 不幸的是,我的代码无法运行,因为在没有TOC的情况下,我正在调用的元素(元素1)超出范围。 知道如何让元素0和1是否存在吗? 这是我的代码。 谢谢!

#Using the period will identify the header; references shouldn't have a period following
docm=re.split('Item 1A\.',doc, re.IGNORECASE)
#Element 0 of list should be the table of contents entry
docm=re.findall('Item 1A\..*?Item',doc, re.IGNORECASE)

#Identifies the next item after 1a (i.e. 1b or 2)
docs=re.sub(' |\.','',doc.split(docm[0])[1][:4]) 
#Finds all text between item 1a. and the next item, element 0 is table of contents, element 1 is what we want
docss=re.findall('item 1a\..*?item '+docs+'\.',doc,re.IGNORECASE)[0-1]

两者都切成薄片,然后取出最后一个。

seq[0:2][-1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM