从生成的数组列表中查找缺失的时间 python
Find missing times from generated array list python
我试图在通过从 MySQL 数据库收集数据生成的列表中查找缺失的时间。这意味着每次函数运行时 test_list 值将始终不同。
我的代码:
def get_time_slotes():
test_list = sorted([u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00'])
res = list(set(range(max(test_list) + 1)) - set(test_list))
print("The list of missing elements : " + str(res))
if __name__ == "__main__":
get_time_slotes()
但是我收到这个错误:
Traceback (most recent call last):
File "/Users/liesching/Documents/test.py", line 41, in <module>
get_time_slotes()
File "/Users/liesching/Documents/test.py", line 36, in get_time_slotes
res = list(set(range(max(test_list) + 1)) - set(test_list))
TypeError: coercing to Unicode: need string or buffer, int found
我正在 Python 2.7 工作,如果这有什么不同的话。这是一个限制,因为我是在一个已经存在的应用程序之上构建的
问题是 max(test_list)
returns u'17:00'
因此您需要将其转换为 17,因为范围采用 int。然后您还需要对 set(test_list)
中的每个项目执行此操作,然后将数字隐藏回时间。示例:
def get_hour(time):
""" u'08:00' -> 8 """
return int(time.split(":")[0])
def get_time(hour):
""" 8 -> u'08:00' """
return (u'%s:00' % hour).zfill(5)
def get_time_slotes():
test_list = sorted([u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00'])
res = map(get_time, list(set(range(get_hour(max(test_list)) + 1)) - set(map(get_hour, test_list))))
print("The list of missing elements : " + str(res))
if __name__ == "__main__":
get_time_slotes()
您的问题在于将 unicode 连接到 int:
u'17:00' + 1
也许如果你解析时间,它会更容易完成任务,比如:
from datetime import datetime
def get_time_slotes():
test_list = [u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00']
test_list = [datetime.strptime(t, '%H:%M').hour for t in test_list]
res = set(range(max(test_list) + 1)) - set(test_list)
print("The list of missing elements : " + str(res))
get_time_slotes()
输出:
The list of missing elements : {0, 1, 2, 3, 4, 5, 6, 7, 11, 14, 15, 16}
我用 datetime 来解决你的问题。首先,我用 utf-8 对每个元素进行编码。
代码不干净,但可以工作!!
from datetime import datetime, timedelta, time
def get_time_slotes():
list_of_times = [u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00']
test_list = sorted(datetime.strptime(x.encode('utf-8'),'%H:%M').time() for x in list_of_times)
res = [x.strftime("%H:%M") for x in sorted(set(time(x,0,0,0) for x in range(max(test_list).hour)) - set(test_list))]
print("The list of missing elements : " + str(res))
输出:
The list of missing elements : ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '11:00', '14:00', '15:00', '16:00']
我试图在通过从 MySQL 数据库收集数据生成的列表中查找缺失的时间。这意味着每次函数运行时 test_list 值将始终不同。
我的代码:
def get_time_slotes():
test_list = sorted([u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00'])
res = list(set(range(max(test_list) + 1)) - set(test_list))
print("The list of missing elements : " + str(res))
if __name__ == "__main__":
get_time_slotes()
但是我收到这个错误:
Traceback (most recent call last):
File "/Users/liesching/Documents/test.py", line 41, in <module>
get_time_slotes()
File "/Users/liesching/Documents/test.py", line 36, in get_time_slotes
res = list(set(range(max(test_list) + 1)) - set(test_list))
TypeError: coercing to Unicode: need string or buffer, int found
我正在 Python 2.7 工作,如果这有什么不同的话。这是一个限制,因为我是在一个已经存在的应用程序之上构建的
问题是 max(test_list)
returns u'17:00'
因此您需要将其转换为 17,因为范围采用 int。然后您还需要对 set(test_list)
中的每个项目执行此操作,然后将数字隐藏回时间。示例:
def get_hour(time):
""" u'08:00' -> 8 """
return int(time.split(":")[0])
def get_time(hour):
""" 8 -> u'08:00' """
return (u'%s:00' % hour).zfill(5)
def get_time_slotes():
test_list = sorted([u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00'])
res = map(get_time, list(set(range(get_hour(max(test_list)) + 1)) - set(map(get_hour, test_list))))
print("The list of missing elements : " + str(res))
if __name__ == "__main__":
get_time_slotes()
您的问题在于将 unicode 连接到 int:
u'17:00' + 1
也许如果你解析时间,它会更容易完成任务,比如:
from datetime import datetime
def get_time_slotes():
test_list = [u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00']
test_list = [datetime.strptime(t, '%H:%M').hour for t in test_list]
res = set(range(max(test_list) + 1)) - set(test_list)
print("The list of missing elements : " + str(res))
get_time_slotes()
输出:
The list of missing elements : {0, 1, 2, 3, 4, 5, 6, 7, 11, 14, 15, 16}
我用 datetime 来解决你的问题。首先,我用 utf-8 对每个元素进行编码。
代码不干净,但可以工作!!
from datetime import datetime, timedelta, time
def get_time_slotes():
list_of_times = [u'08:00', u'12:00', u'13:00', u'09:00', u'10:00', u'17:00']
test_list = sorted(datetime.strptime(x.encode('utf-8'),'%H:%M').time() for x in list_of_times)
res = [x.strftime("%H:%M") for x in sorted(set(time(x,0,0,0) for x in range(max(test_list).hour)) - set(test_list))]
print("The list of missing elements : " + str(res))
输出:
The list of missing elements : ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '11:00', '14:00', '15:00', '16:00']