我有一个列表,其句点用<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link rel="shortcut icon" type="image/png" href="assets/cv2.png">
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-expand-sm navbar-dark fixed-top">
<a href="#" class="navbar-brand">Home</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarMenu">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Portfolio</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link"><i class="fa fa-envelope"></i> Contact</a>
</li>
</ul>
</div>
</nav>
<main class="content-wrapper">
<header class="white-text-container section-container">
<div class="text-center">
<h1>Lorem ipsum</h1>
<p>Lorem ipsum</p>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="card">
<div class="card-block">
<h2>About</h2>
<div class="row">
<div class="col-md-4">
<p><img src="./assets/images/img-01.jpg" class="img-responsive" alt=""></p>
</div>
<div class="col-md-8">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
</div>
</main>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>
值分隔,我需要创建一个列表列表,其中每个子列表都包含相应时间段的最小和最大日期。
示例:
None
所需的输出:
import datetime
dates = [None, None, datetime.date(2018, 1, 11), datetime.date(2018, 3, 2), datetime.date(2018, 4, 30), datetime.date(2018, 5, 1), None, datetime.date(2018, 5, 30), datetime.date(2018, 6, 3), datetime.date(2018, 6, 4), None, None]
我该怎么做?
答案 0 :(得分:4)
您可以使用T3
(doc)将元素分组为itertools.groupby
和None
值,然后使用date
,min()
函数:
max()
打印:
import datetime
dates = [None, None, datetime.date(2018, 1, 11), datetime.date(2018, 3, 2), datetime.date(2018, 4, 30), datetime.date(2018, 5, 1), None, datetime.date(2018, 5, 30), datetime.date(2018, 6, 3), datetime.date(2018, 6, 4), None, None]
from itertools import groupby
out = []
for v, g in groupby(dates, lambda k: k is None):
if v:
continue
l = [*g]
out.append([min(l), max(l)])
from pprint import pprint
pprint(out)
编辑:感谢@BlackJack的建议,可以简化此过程:
[[datetime.date(2018, 1, 11), datetime.date(2018, 5, 1)],
[datetime.date(2018, 5, 30), datetime.date(2018, 6, 4)]]
答案 1 :(得分:0)
这是我的看法。此解决方案不需要任何其他导入,并且从逻辑上讲对初学者友好。 以下是其工作原理:
您在迭代列表时忽略了前两个和后None
个值。
在列表中或None
之后遇到第一个值时,请将最小值和最大值都设置为该值,因为这表示新时期的开始。
之后,对于遇到的每个值,将其与先前的最小值和最大值进行比较,并将其设置为新的最小值或最大值(如果更低或更高)。
遇到None
后,您将当前的最小值和最大值保存到结果列表中并进行重置,因为这意味着新的周期将从下一个值开始。
import datetime
dates = [None, None, datetime.date(2018, 1, 11), datetime.date(2018, 3, 2), datetime.date(2018, 4, 30),
datetime.date(2018, 5, 1), None, datetime.date(2018, 5, 30), datetime.date(2018, 6, 3),
datetime.date(2018, 6, 4), None, None]
min_date = None
max_date = None
result = []
for value in dates[2:-1]:
if value is None:
result.append([min_date, max_date])
min_date = None
max_date = None
elif min_date is None and max_date is None:
min_date = value
max_date = value
elif value < min_date:
min_date = value
elif value > max_date:
max_date = value
print(result)
输出:
[[datetime.date(2018, 1, 11), datetime.date(2018, 5, 1)], [datetime.date(2018, 5, 30), datetime.date(2018, 6, 4)]]
希望您从中学到了一些东西。