Posts: 12
Threads: 5
Joined: Apr 2019
Dear Python Developers,
I hope you are having a great saturday. Could you please assert me if I'm coding this correct:
The goal is to convert my list into a library:
1) The code works but it provides me the same key for all items in the list. I'm I doing this right?
#Add list of stocks to dictionary
x = [("GOOGL", 125, 772.88, 941.53),
("MSFT", 85, 56.60, 73.04),
("RDS-A", 400, 49.58, 55.74),
("AIG", 235, 54.21, 65.27),
("FB", 150, 124.31, 172.45)]
mydata = {}
for i in range(len(x)):
mydata[x[i]] = x.count(x[i])
print (mydata) Output: {('GOOGL', 125, 772.88, 941.53): 1, ('MSFT', 85, 56.6, 73.04): 1, ('RDS-A', 400, 49.58, 55.74): 1, ('AIG', 235, 54.21, 65.27): 1, ('FB', 150, 124.31, 172.45): 1}
Posts: 479
Threads: 86
Joined: Feb 2018
len probably finds the amount of tuples in the list. It then puts the tuples in the dictionary, Try using len twice to find everything inside all the tuples in the list
Posts: 12
Threads: 5
Joined: Apr 2019
Dear SheeppOSU,
First of all thank you for taking time out of your busy time to respond.
Could you please be more specific recoding that line.
Kind Regards,
Jehu
Posts: 4,220
Threads: 97
Joined: Sep 2016
Your terminology is incorrect. You are trying to make a dictionary, not a library. And your values are the same (1), not your keys. Keys by definition are different. If they were all the same key.
How to fix this is not clear, because what you want is not clear. Do you want the stock names a keys with the numbers as values?
mydata = {}
for stock in x:
mydata[stock[0]] = stock[1:]
Posts: 479
Threads: 86
Joined: Feb 2018
I don't know exactly what you're trying to do but if you're trying to put each of these in a dictionary and give them the 1. This is how you would do that -
x = [("GOOGL", 125, 772.88, 941.53),
("MSFT", 85, 56.60, 73.04),
("RDS-A", 400, 49.58, 55.74),
("AIG", 235, 54.21, 65.27),
("FB", 150, 124.31, 172.45)]
mydata = {}
for i in x:
for f in i:
mydata.update({f : 1})
print(mydata) Output: {'GOOGL': 1, 125: 1, 772.88: 1, 941.53: 1, 'MSFT': 1, 85: 1, 56.6: 1, 73.04: 1, 'RDS-A': 1, 400: 1, 49.58: 1, 55.74: 1, 'AIG': 1, 235: 1, 54.21: 1, 65.27: 1, 'FB': 1, 150: 1, 124.31: 1, 172.45: 1}
Posts: 12
Threads: 5
Joined: Apr 2019
Apr-21-2019, 01:44 AM
(This post was last modified: Apr-21-2019, 01:44 AM by prophet11.)
Thank you Ichabod801. Yes I meant dictionary, not library. Yes, I wanted the stock names as keys with the numbers as values. I really, really appreciate your help.
SheeppOSU. Thank you for your help.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Apr-22-2019, 04:49 AM
(This post was last modified: Apr-22-2019, 04:49 AM by perfringo.)
There is always a question what you want to do with resulting dictionary. You can do as ichabod801 suggested:
>>> x = [("GOOGL", 125, 772.88, 941.53),
... ("MSFT", 85, 56.60, 73.04),
... ("RDS-A", 400, 49.58, 55.74),
... ("AIG", 235, 54.21, 65.27),
... ("FB", 150, 124.31, 172.45)]
>>> my_data = {}
>>> for stock in x:
... my_data[stock[0]] = stock[1:]
...
>>> my_data
{'GOOGL': (125, 772.88, 941.53),
'MSFT': (85, 56.6, 73.04),
'RDS-A': (400, 49.58, 55.74),
'AIG': (235, 54.21, 65.27),
'FB': (150, 124.31, 172.45)} Now, if you want to access first value, then you must use index like this:
>>> my_data['GOOGL'][0]
125 It could become quite complicated to remember all indexes and their meaning if code is (becomes) lengthy.
One way is to use dictonary as value with descriptive names:
>>> my_data = {}
>>> headers = ('quantity', 'purchase price', 'current price')
>>> for stock in x:
... my_data[stock[0]] = dict(zip(headers, stock[1:]))
...
>>> my_data
{'GOOGL': {'quantity': 125, 'purchase price': 772.88, 'current price': 941.53},
'MSFT': {'quantity': 85, 'purchase price': 56.6, 'current price': 73.04},
'RDS-A': {'quantity': 400, 'purchase price': 49.58, 'current price': 55.74},
'AIG': {'quantity': 235, 'purchase price': 54.21, 'current price': 65.27},
'FB': {'quantity': 150, 'purchase price': 124.31, 'current price': 172.45}}
>>> my_data['GOOGL']['quantity']
125
>>> sum(row['quantity'] for row in my_data.values()) # total number of shares
995
>>> sum(row['quantity'] * row['current price'] for row in my_data.values()) # portfolio total value
187401.6 Another possibility is to have list of dictionaries:
>>> headers = ('name', 'quantity', 'purchase price', 'current price')
>>> my_data = []
>>> for stock in x:
... my_data.append(dict(zip(headers, stock)))
...
>>> my_data
[{'name': 'GOOGL',
'quantity': 125,
'purchase price': 772.88,
'current price': 941.53},
{'name': 'MSFT',
'quantity': 85,
'purchase price': 56.6,
'current price': 73.04},
{'name': 'RDS-A',
'quantity': 400,
'purchase price': 49.58,
'current price': 55.74},
{'name': 'AIG',
'quantity': 235,
'purchase price': 54.21,
'current price': 65.27},
{'name': 'FB',
'quantity': 150,
'purchase price': 124.31,
'current price': 172.45}]
>>> sum(row['quantity'] for row in my_data) # number of shares
995
>>> sum(row['quantity'] * row['current price'] for row in my_data) # portfolio total value
187401.6
>>> sum((row['current price'] - row['purchase price']) * row['quantity'] for row in my_data) # portfolio total profit
34762.74999999999
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|