【问题标题】:for loop, split string, save parts of string to new list, IndexError: list index out of range - works for one part of string and not the otherfor 循环,拆分字符串,将部分字符串保存到新列表,IndexError:列表索引超出范围 - 适用于字符串的一部分而不是另一部分
【发布时间】:2019-06-20 06:14:12
【问题描述】:

python 新手。我有一个在 for 循环中拆分的文件名列表。我想提取其中的一些列并将它们放入一个新列表中。它适用于 x[0],但不适用于 x[6]。当我打印 x[0] 和 x[6] 时,它们都是字符串并且都有一个值。

p_type=[]; p_start_time=[]; p_end_time=[];

for i in precise_links: #precise_links is list of filenames
    x = i.split('_')
    print(x)
    p_type_x = x[0]; p_type.append(p_type_x)
    p_start_time_x = x[6]; p_start_time.append(p_start_time_x)

#To show you x
print(x)

#To show you what each x part is/type
print(x[0]) 
type(x[0])
print(x[6])
type(x[6])

print(p_type)

输出

['S1A', 'OPER', 'AUX', 'POEORB', 'OPOD', '20180829T120853', 'V20180808T225942', '20180810T005942.EOF']
['S1A', 'OPER', 'AUX', 'POEORB', 'OPOD', '20171021T121400', 'V20170930T225942', '20171002T005942.EOF']
['S1A', 'OPER', 'AUX', 'POEORB', 'OPOD', '20150525T122539', 'V20150503T225944', '20150505T005944.EOF']
['S1A', 'OPER', 'AUX', 'POEORB', 'OPOD', '20180703T120727', 'V20180612T225942', '20180614T005942.EOF']
['S1B', 'OPER', 'AUX', 'POEORB', 'OPOD', '20171015T111433', 'V20170924T225942', '20170926T005942.EOF']
['S1A', 'OPER', 'AUX', 'POEORB', 'OPOD', '20150605T122809', 'V20150514T225944', '20150516T005944.EOF']
....

['S1B', 'OPER', 'AUX', 'POEORB', 'OPOD', '20160620T141641', 'V20160528T225943', '20160530T005943.EOF']

S1B

str

V20160528T225943

str

['S1A', 'S1A', 'S1A', 'S1A', 'S1B', 'S1A', 'S1B', 'S1B', 'S1A'...]

我在运行循环时遇到的错误。

---------------------------------------------------------------------------
IndexError - Traceback (most recent call last)

<ipython-input-131-2cbe5599886f> in <module>= 13     
p_start_time_x = x[6]; #p_start_time.append(p_start_time_x)

IndexError: list index out of range

【问题讨论】:

  • 你能在循环中打印 x 吗?就在分手之后。我认为问题在于某些 x 没有 7 个元素(索引 6)。我不知道为什么 x 是什么,但 python 告诉你,有时你的 split 没有返回足够的元素。
  • 在输出中添加(只是一个快照(超过 200 行),每行有 8 个元素。哦,我看到一个空元素。将删除该行并更新结果。
  • 在错误行前添加print(i) (p_start_time_x = x[6];) 以了解错误来自哪个迭代。似乎其中一个文件名太短了。错误前的最后一次打印将显示有问题的文件。

标签: python list


【解决方案1】:

您的问题是某些文件名没有足够的下划线分隔部分。假设如果他们真的不这样做,start_time 元素不相关,你可以这样做:

p_type=[]
p_start_time=[]

for i in precise_links: #precise_links is list of filenames
    x = i.split('_')
    try:
        p_type.append(x[0])
        p_start_time.append(x[6])
    except IndexError:
        print("Bad file encountered - {}".format(i)
        continue

【讨论】:

  • 如果,太短,你的意思是他们有少于 6 个下划线分隔字符串中的字符,是的。为了调试,您可以输入 print(x),而不是继续,并检查少于 7 个元素的列表的任何输出。
【解决方案2】:

我最终做了什么.. 所有文件名的顺序都应该正确,我希望我的列表长度相同(以便以后使用),所以我只是丢弃了任何不符合条件的名称(长度为 8)

p_type=[]; p_start_time=[]; p_end_time=[];

for i in precise_links:
    x = i.split('_')
    if len(x) == 8:
        p_type.append(x[0])
        p_start_time.append(x[6])

【讨论】:

  • 修复x = i.split('_')的缩进
猜你喜欢
  • 2020-10-16
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2018-12-11
  • 2019-04-16
相关资源
最近更新 更多