如何在Python中创建映射列表(数组)(关联子数组)

时间:2012-12-13 16:26:29

标签: python list dictionary python-2.7 mapping

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

2 个答案:

答案 0 :(得分:3)

您的列表和字典文字定义可以大大简化:

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

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

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

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

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

我怀疑你首先阅读(优秀)Python tutorial会受益。

答案 1 :(得分: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']

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