2 回答
TA贡献1850条经验 获得超11个赞
你在寻找吗?如下所示:
for i, val in enumerate(time_d):
if not val:
time_d[i] = time_d[i-1]
time_d
# ['Monday 30 March', 'Monday 30 March', ... ,'Monday 30 March',
# 'Tuesday 31 March', 'Tuesday 31 March', ... , 'Tuesday 31 March',
# 'Wednesday 1 April', 'Wednesday 1 April', ..., 'Wednesday 1 April']
TA贡献2041条经验 获得超4个赞
这是一个简单的解决方案(在Python 3中):
dates = ['Monday 30 March', '', '', '', '', '', '', '', '', '', 'Tuesday 31 March', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Wednesday 1 April', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
newdates = []
current = ''
for item in dates:
if not item:
newdates.append(current)
else:
current = item
newdates.append(current)
print(dates)
print(newdates)
带输出:
['Monday 30 March', '', '', '', '', '', '', '', '', '', 'Tuesday 31 March', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'Wednesday 1 April', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
['Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Monday 30 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Tuesday 31 March', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April', 'Wednesday 1 April']
添加回答
举报