1

我是 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
'''
4

2 回答 2

3

您的 list 和 dict 文字定义可以大大简化:

keys = ['ticker', 't_date', 't_open', 't_high', 't_low', 't_close', 't_volume']
arr1 = [
    dict.fromkeys(keys, ' '),
    dict.fromkeys(keys, ' ')
]

我正在使用该dict.fromkeys()方法用一系列键初始化字典,所有键都具有给定的默认值(一个空格字符串)。

在 Python 中定义空列表时,不能简单地寻址不存在的元素。或者,使用该.append()方法将元素添加到列表中:

arr1.append({'key': 'value', 'otherkey': 'othervalue'})

上面的示例使用{k: v}dict 文字表示法。

我怀疑你会从首先阅读(优秀的)Python 教程中受益。

于 2012-12-13T16:31:18.607 回答
0

以下是我学到的,对于那些与我有相似编程背景的人:

arr0 = dict( [ ('one',1), ('two',2), ('three',3) ] )
for k,v in arr0.iteritems() : 
    print k,v           #prints the associative array key with the value

keys = ['ticker','t_open','t_high','t_low','t_close','t_volume']
arr1 = [
            dict.fromkeys(keys,' '),
            dict.fromkeys(keys,' ')
        ]

arr1[0]['t_volume'] = 11250000
arr1[1]['t_volume'] = 11260000
arr1.append( {'ticker' : 'ibm', 't_date' : '1/2/2008', 't_volume' : 11270000} )
arr1.insert(3, {'ticker' : 'ibm', 't_date' : '1/3/2008', 't_volume' : 11280000} )

print "\nAssociative array inside of an INT indexed array:"
print arr1[0]['t_volume'], arr1[1]['t_volume']
print "\n ",arr1[2]
print arr1[2]['t_volume'], arr1[3]['t_volume']

注意元素(值)添加逻辑。

于 2012-12-13T18:35:09.150 回答