Use defaultdict, not Counter for travel distances

This commit is contained in:
Edward Betts 2024-05-27 10:13:24 +02:00
parent f8c523c674
commit cd8dfb74a4

View file

@ -3,7 +3,7 @@
import collections import collections
import datetime import datetime
import typing import typing
from collections import Counter from collections import defaultdict
from dataclasses import dataclass, field from dataclasses import dataclass, field
import emoji import emoji
@ -190,12 +190,12 @@ class Trip:
Any travel item with a missing or None 'distance' field is ignored. Any travel item with a missing or None 'distance' field is ignored.
""" """
transport_distances: Counter[float] = Counter() transport_distances: defaultdict[str, float] = defaultdict(float)
for item in self.travel: for item in self.travel:
distance = item.get("distance") distance = item.get("distance")
if distance: if distance:
transport_type = item.get("type", "unknown") transport_type: str = item.get("type", "unknown")
transport_distances[transport_type] += distance transport_distances[transport_type] += distance
return list(transport_distances.items()) return list(transport_distances.items())