Dicționare

Python intermediar pentru finanțe

Kennedy Behrman

Data Engineer, Author, Founder

Căutare după index

my_list = ['a','b','c','d']
  0   1   2   3
['a','b','c','d']
my_list[0]
'a'
my_list.index('c')
2
Python intermediar pentru finanțe

Căutare după cheie

Dicționare

Diagramă cu chei care indică valori

Python intermediar pentru finanțe

Reprezentare

{ 'key-1':'value-1', 'key-2':'value-2', 'key-3':'value-3'}
Python intermediar pentru finanțe

Crearea dicționarelor

my_dict = {}
my_dict
{}
my_dict = dict()
my_dict
{}
Python intermediar pentru finanțe

Crearea dicționarelor

ticker_symbols = {'AAPL':'Apple', 'F':'Ford', 'LUV':'Southwest'}
print(ticker_symbols)
{'AAPL':'Apple', 'F':'Ford', 'LUV':'Southwest'}
ticker_symbols = dict([['APPL','Apple'],['F','Ford'],['LUV','Southwest']])
print(ticker_symbols)
{'AAPL':'Apple', 'F':'Ford', 'LUV':'Southwest'}
Python intermediar pentru finanțe

Adăugarea în dicționare

ticker_symbols['XON'] = 'Exxon'
ticker_symbols
{'APPL': 'Apple', 'F': 'Ford', 'LUV': 'Southwest', 'XON': 'Exxon'}
ticker_symbols['XON'] = 'Exxon OLD'
ticker_symbols
{'APPL': 'Apple','F': 'Ford','LUV': 'Southwest','XON': 'Exxon OLD'}
Python intermediar pentru finanțe

Accesarea valorilor

ticker_symbols['F']
'Ford'
Python intermediar pentru finanțe

Accesarea valorilor

ticker_symbols['XOM']
KeyError                                  Traceback (most recent call last)
<ipython-input-6-782fbf617bf7> in <module>()
-> 1 ticker_symbols['XOM']

   KeyError: 'XOM'
Python intermediar pentru finanțe

Accesarea valorilor

company = ticker_symbols.get('LUV')
print(company)
'Southwest'
company = ticker_symbols.get('XOM')
print(company)
None
company = ticker_symbols.get('XOM', 'MISSING')
print(company)
'MISSING'
Python intermediar pentru finanțe

Ștergerea din dicționare

ticker_symbols
{'APPL': 'Apple', 'F': 'Ford', 'LUV': 'Southwest', 'XON': 'Exxon OLD'}
del(ticker_symbols['XON'])
ticker_symbols
{'APPL': 'Apple', 'F': 'Ford', 'LUV': 'Southwest'}
Python intermediar pentru finanțe

Să exersăm!

Python intermediar pentru finanțe

Preparing Video For Download...