I am trying to turn a nested list:
#stock[symbol,price,amount,side]
stocks = [["AAPL",10,5,0],["AAPL",15,2,1],["AAPL",25,1,1],["AAPL",30,4,0],
["TSLA",10,5,0],["TSLA",15,2,1],["TSLA",25,1,1],["TSLA",30,4,0]]
into a dictionary like:
#res = {"symbol":{price:total_amount,price:total_amount}
res = {"AAPL":{10:5,15:2,25:1,30:4},"TSLA":{10:5,15:2,25:1,30:4}}
The subdict is calculated by:
subdict ={}
for s,p,a,side in stocks:
subdict[p] = subdict.get(p,0)+a
The share returns total_share.e.g. if stocks = [["AAPL",10,3,0],["AAPL",10,3,0]] should return {"AAPL":{10:6}}
for s, p, a, s ...as You can thensrepeats twice, also does unpacking like that even work?["AAPL",10,5,0]the zero will be ignored?