每小时处理的Python编号

时间:2017-07-19 22:12:04

标签: python python-3.x

我能够在excel中使用它,但是我似乎找不到Python 3.X的解决方案。如何计算每小时处理的数量?

我需要创建一个公式来确定1小时内生产的单位数量。公式unitsPerHour = totalProcessed/(totalTime*24)适用于excel,但不适用于Python。似乎totalTime*24总是产生0

示例:花了4小时26分3秒(04:26:03)并完成了73个单位。每小时完成了多少单位?

# "%d:%d:%d" % (diff.hours, diff.minutes, diff.seconds) = 04:26:03
totalTime = "%d:%d:%d" % (diff.hours, diff.minutes, diff.seconds)
totalProcessed = 73
denomCheck = totalTime*24
if denomCheck != 0:
    unitsPerHour =  totalProcessed/(totalTime*24)

当然,只要分母等于零,就会抛出错误。 denomCheck变量的目的是什么。我确实有totalTime等于零的实例,所以我必须检查它。

2 个答案:

答案 0 :(得分:0)

很简单,您从未将diff变量从三倍值转换为数字小时数。相反,您将其格式化为人类可读的字符串。将一个字符串乘以24可以将24个副本连接成一个字符串...在发布之前,使用简单的print语句进行验证。例如:

# "%d:%d:%d" % (diff.hours, diff.minutes, diff.seconds) = 04:26:03
totalTime = "%d:%d:%d" % (4, 26, 3)
totalProcessed = 73
denomCheck = totalTime*24
print "totalTime", totalTime
print "denomCheck", denomCheck
if denomCheck != 0:
    unitsPerHour =  totalProcessed/(totalTime*24)

输出:

totalTime 4:26:3
denomCheck 4:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:34:26:3
Traceback (most recent call last):
  File "so.py", line 8, in <module>
    unitsPerHour =  totalProcessed/(totalTime*24)
TypeError: unsupported operand type(s) for /: 'int' and 'str'

解决方案:

算一算:

total_time = diff.hours + diff.minutes/60.0 + diff.seconds/3600.0

如果您想学习操作使这些事情变得更容易的数据类型,请尝试使用Python的datetime包。

答案 1 :(得分:0)

给定像'04:26:03'这样的字符串,您可以执行以下操作来计算每小时的单位:

time_string = '04:26:03'

# extract hours, minutes, and seconds
hours, minutes, seconds =  [int(item.lstrip('0')) for item in time_string.split(':')]

# calculate total time, assuming Python 3
total_time_in_hours = hours + minutes/60 + seconds/3600

# units per hour
units_per_hour = totalProcessed / total_time_in_hours

如果值以整数形式提供, 只需这样做:

total_time_in_hours = diff.hours + diff.minutes / 60 + diff.seconds / 3600
units_per_hour = totalProcessed / total_time_in_hours