0

timedeltas を含む 2 つのカウンターを追加しようとしています。カウンターを追加すると、次のことが発生します。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/collections/__init__.py", line 734, in __add__
    if newcount > 0:
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

これにより例外が発生します。

from collections import Counter
from datetime import timedelta
a = Counter(time=timedelta(microseconds=167242))
a + a

ただし、これはしません:

b = timedelta(microseconds=167242)
b + b
4

1 に答える 1

0

A few things are getting in the way:

  1. Counter is designed to do discrete counting, that is, the values are integers.
  2. timedeltas don't define what it means to be compared to zero, though they could have.

Looks like you will need to do your own Counter-like structure.

于 2019-05-12T15:40:30.727 に答える