我是 Python 的一个 n00b 并且正在尝试将我从 shell 和 PHP 脚本中获得的一点点知识带到 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
'''