【发布时间】:2018-12-04 17:01:08
【问题描述】:
我有一个列表,其中包含几百个日期,但我只需要过滤掉 hhmmss 格式的时间(没有 : 介于两者之间,以便稍后将它们转换为 int)。
到目前为止我的列表如下所示:
list_dates = ['Fri Nov 30 15:56:43 +0000 2018', 'Fri Nov 30 15:56:44 +0000 2018', 'Fri Nov 30 15:56:45 +0000 2018', 'Fri Nov 30 15:56:46 +0000 2018', ... ]
到目前为止,我的方法是:
raw_list = []
new_list = []
def ConversionStrings():
j = 0
while j < (len(list_dates)+1):
for j in list_dates:
raw_list.append((list_dates[j])[11:19])
j += 1
i = 0
while i < len(raw_list):
for i in raw_list:
item = (raw_list[i]).replace(":", "")
new_list.append(item)
i += 1
显然这不起作用,但我不知道我做错了什么,因为我对 Python 还很陌生。我得到的错误是this,它指的是for循环中的j
TypeError: list indices must be integers or slices, not str
如何更改我的代码以获得此输出?或者有没有更简单的方法来解决这个问题?
>>> new_list = [155643, 155644, 155645, ... ]
【问题讨论】: