Skip to content

Instantly share code, notes, and snippets.

@bison--
Created July 24, 2022 10:56
Show Gist options
  • Save bison--/9505a3a2e5cc1365c45df1fee9d56c23 to your computer and use it in GitHub Desktop.
Save bison--/9505a3a2e5cc1365c45df1fee9d56c23 to your computer and use it in GitHub Desktop.
list counter
import random
class ItemInfo:
def __init__(self, place=0, amount=0):
self.place = place
self.amount = amount
def __repr__(self):
return '{0}: {1}'.format(self.place, self.amount)
def count_by_place(items: [ItemInfo]):
counted_items = {}
for item in items:
if item.place not in counted_items:
counted_items[item.place] = 0
counted_items[item.place] += item.amount
return counted_items
item_list = []
for i in range(5):
item_list.append(
ItemInfo(
random.randint(1, 3),
random.randint(4, 5)
)
)
print(item_list)
print(count_by_place(item_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment