I am trying to split this array in Python that I pulled down from a DynamoDB table into separate chunks so that I can then calculate the Averages of each AssetID, storing the AssetID and the Rating into separate parts of the Array.
[{'ID': Decimal('0'), 'AssetID': Decimal('0'), 'Rating': Decimal('3')}, {'ID': Decimal('1'), 'AssetID': Decimal('0'), 'Rating': Decimal('5')}]
So for example, I would like the array to look like the following:
AssetIDArray[AssetID][Rating]
So hence if I were to print the following, I would get 5.
AssetIDArray[0][1]
Thanks in advance for the help!
You need a dict instead of a list as AssetIDs:
from decimal import Decimal
items = [{'ID': Decimal('0'), 'AssetID': Decimal('0'), 'Rating': Decimal('3')},
{'ID': Decimal('1'), 'AssetID': Decimal('0'), 'Rating': Decimal('5')}]
parsed_item_list = {}
for item in items:
assetId = int(item.get('AssetID'))
rating = int(item.get('Rating'))
if assetId not in parsed_item_list:
parsed_item_list[assetId] = []
parsed_item_list[assetId].append(rating)
print(parsed_item_list.get(0)[0]) # 3
print(parsed_item_list.get(0)[1]) # 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.