我有两个包含时间戳的列表。我想比较并使用时间戳来创建一个新列表。
comp = ['2018-07-21,11:37:20', '2018-07-21,11:52:57', '2018-07-21,12:08:20', '2018-07-21,12:41:28', '2018-07-21,12:56:46', '2018-07-21,13:12:09', '2018-07-22,11:12:50', '2018-07-22,11:29:18', '2018-07-22,11:44:44', '2018-07-22,12:08:57', '2018-07-22,12:24:18', '2018-07-22,12:39:35']
startDate = ['2018-07-21,11:37:07', '2018-07-22,11:12:41']
我正在尝试创建
start = ['2018-07-21,11:37:07', '2018-07-21,11:37:07', '2018-07-21,11:37:07', '2018-07-21,11:37:07', '2018-07-21,11:37:07', '2018-07-21,11:37:07', '2018-07-22,11:12:41', '2018-07-22,11:12:41', '2018-07-22,11:12:41', '2018-07-22,11:12:41', '2018-07-22,11:12:41', '2018-07-22,11:12:41']
其长度与 comp
相同,但使用 startDate
中的元素。 start[i]
将是 startDate
中的元素之一,具体取决于 comp[i]
是否大于或等于 startDate
.
我目前拥有的代码是:
start = []
for x in comp:
for y in range(len(startDate)-1):
if x >= startDate[y] and x < startDate[y+1]:
start.append(startDate[y])`
但是start
的长度只有6(我期望的前六个元素)
感谢任何及所有帮助!
最佳答案
for x in comp:
for y in range(len(startDate)-1):
if x >= startDate[y] and x < startDate[y+1]:
start.append(startDate[y])
else:
start.apped(startDate[y+1])
这应该有效。
关于python - Python 嵌套 For 循环的时间比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732697/