python - Convert AM,PM to 24 in a nested list using python
问题描述
I have a nested list with AM,PM time format. I want to convert these to 24 hours format. I wrote a code that convert the times to 24 but the result is not true. What is wrong in my code?
mylist = [
['x', '06:00 - 09:30 AM - 10:30 AM , 02:00 PM - 05:00 PM - 09:00 PM'],
['y', '07:30 AM - 02:30 PM, 07:30 AM , 02:30 PM, 07:30 AM - 01:30 PM']
]
correctResult= [['x', '06:00 - 09:30 AM - 10:30 AM - 14:00 PM - 17:00 PM - 21:00 PM'],
['y', '07:30 AM - 14:30 PM, 07:30 AM - 14:30 PM, 07:30 AM - 13:30 PM']]
myresult = [
['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['x', ['06:00', '09:30', '10:30', '14:00', '17:00', '21:00']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']], ['y', ['07:30', '02:30 PM, 07:30 AM', '02:30 PM, 07:30 AM', '13:30']]
]
The code is as below:
def time24(timepmam):
from datetime import datetime
in_time = datetime.strptime(timepmam, "%I:%M %p")
out_time = datetime.strftime(in_time, "%H:%M")
return out_time
result = []
for x in mylist:
res = x[0]
ss = []
for y in x[1].split(' - '):
# print(y)
if len(y) > 5 and len(y)<9:
print(y)
t = time24(y)
ss.append(t)
else:
ss.append(y)
s = ' '.join(ss)
result.append([res, ss])
print(result)
解决方案
This is pattern matching, so I probably would go for regex to find those times to be replaced:
mylist = [
['x', '06:00 - 09:30 AM - 10:30 AM , 02:00 PM - 05:00 PM - 09:00 PM'],
['y', '07:30 AM - 02:30 PM, 07:30 AM , 02:30 PM, 07:30 AM - 01:30 PM']
]
from datetime import datetime
import re
def time24(timepmam):
# this function is only called for times that have PM in them
# adding PM to a 24h time does not make sense at all - but can be done
in_time = datetime.strptime(timepmam, "%I:%M %p")
out_time = datetime.strftime(in_time, "%H:%M PM") # add the PM as well
return out_time
def fix(s):
# only find & fix PM`s
pattern = r"\d\d:\d\d PM"
for match in re.findall(pattern,s):
s = re.sub(match, time24(match), s)
return s
print(mylist)
for l in mylist:
for idx,p in enumerate(l):
l[idx] = fix(p)
print(mylist)
Output:
# before
[['x', '06:00 - 09:30 AM - 10:30 AM , 02:00 PM - 05:00 PM - 09:00 PM'],
['y', '07:30 AM - 02:30 PM, 07:30 AM , 02:30 PM, 07:30 AM - 01:30 PM']]
# after
[['x', '06:00 - 09:30 AM - 10:30 AM , 14:00 PM - 17:00 PM - 21:00 PM'],
['y', '07:30 AM - 14:30 PM, 07:30 AM , 14:30 PM, 07:30 AM - 13:30 PM']]
推荐阅读
- r - 随机顶点样本的边数(igraph)
- python - Python ntplib 响应中的所有字段是什么,它们是如何使用的?
- javascript - Rollup 希望我创建全局变量。如何使用“导出默认值”?
- javascript - Gatsby 导入 babel polyfill
- excel - 使用excel计算日期范围之间的年龄
- angular - Angular 6 + Bootstrap 4 隐藏 ngFor div
- jmeter - jmeter ${__threadNum} 通过命令行参数传递时不呈现
- qt - QML 中的材质样式对话框
- python - KerasRegression 模型中的“TypeError:无法腌制 NotImplementedType 对象”
- python - 使用带有 NLTK 的块标签(不是 NER)在句子中创建关系 | 自然语言处理