我是Python的n00b,並試圖將我從shell和PHP腳本中獲得的知識帶入Python。我真的很想掌握創建和操作內部值的概念(同時保持代碼的可理解形式)。如何在Python中創建映射列表(關聯子數組)
我無法使用LISTS和MAPPINGS(dict())的Python實現。我正在編寫一個腳本,需要在基本數組(Python列表)內使用關聯數組(一個python映射)。該列表可以使用典型的INT索引。
謝謝!
這裏是我目前:
''' Marrying old-school array concepts
[in Python verbiage] a list (arr1) of mappings (arr2)
[per my old-school training] a 2D array with
arr1 using an INT index
arr2 using an associative index
'''
arr1 = []
arr1[0] = dict([ ('ticker'," "),
('t_date'," "),
('t_open'," "),
('t_high'," "),
('t_low'," "),
('t_close'," "),
('t_volume'," ")
])
arr1[1] = dict([ ('ticker'," "),
('t_date'," "),
('t_open'," "),
('t_high'," "),
('t_low'," "),
('t_close'," "),
('t_volume'," ")
])
arr1[0]['t_volume'] = 11250000
arr1[1]['t_volume'] = 11260000
print "\nAssociative array inside of an INT indexed array:"
print arr1[0]['t_volume'], arr1[1]['t_volume']
在PHP中,我有以下示例工作:
'''
arr_desired[0] = array('ticker' => 'ibm'
't_date' => '1/1/2008'
't_open' => 123.20
't_high' => 123.20
't_low' => 123.20
't_close' => 123.20
't_volume' => 11250000
);
arr_desired[1] = array('ticker' => 'ibm'
't_date' => '1/2/2008'
't_open' => 124.20
't_high' => 124.20
't_low' => 124.20
't_close' => 124.20
't_volume' => 11260000
);
print arr_desired[0]['t_volume'],arr_desired[1]['t_volume'] # should print>>> 11250000 11260000
'''
如果你已經知道一些編程語言;給[python教程試試](http://docs.python.org/2/tutorial/) – jfs