python - Python 嵌套 For 循环的时间比较

标签 python python-3.x nested

我有两个包含时间戳的列表。我想比较并使用时间戳来创建一个新列表。

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/

相关文章:

python - python 需要 __init__.py 包的原因是什么?

python - pyodbc 执行变量变为@P1

python - 多处理比顺序处理慢

Ruby:合并嵌套哈希

linq - 使用 LINQ 从嵌套集合中选择 Single

python - 第一个未命名参数可选

python - Django - 在子路径中运行时静态文件的问题

python-3.x - PUBLIC属性和方法(作用于公共(public)属性)可以被视为封装吗?

python - 在其类 block 之外定义对象方法,有什么理由不这样做?

python - 访问嵌套在字典中的值