Created
July 24, 2022 10:56
-
-
Save bison--/9505a3a2e5cc1365c45df1fee9d56c23 to your computer and use it in GitHub Desktop.
list counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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