背景:
我有一个python脚本来检查员工的工作时间。每个员工都有早班和下午班,中间有午餐时间,每次他们伸出手指,都会记录一个新的时间戳。你知道吗
因此,根据每天的时间,列表中可能有0到4个时间戳。你知道吗
问题是:“如何将时间戳“解压”到相应的变量,以避免所有这些丑陋的、重复的代码?”你知道吗
morning_entry = None
morning_leave = None
afternoon_entry = None
afternoon_leave = None
timestamps = get_timestamps()
if timestamps:
morning_entry = timestamps.pop(0)
if timestamps:
morning_leave = timestamps.pop(0)
if timestamps:
afternoon_entry = timestamps.pop(0)
if timestamps:
afternoon_leave = timestamps.pop(0)
一个简单的解决方案,但可能不是那么优雅
只需在列表前填充Nones,然后切片
基于
itertools
的Sainath's answer版本:它是否更优雅值得商榷;它确实有一点小小的改进,即
timestamps
不局限于任何特定类型的iterable。你知道吗相关问题 更多 >
编程相关推荐