2012-12-13 138 views
1

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

如果你已經知道一些編程語言;給[python教程試試](http://docs.python.org/2/tutorial/) – jfs

回答

3

你的列表和字典字面定義,可以大大簡化:

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}字典的文字符號。

我懷疑你會受益於首先閱讀(優秀)Python tutorial

+0

謝謝!

The dict.fromkeys(keys,' ')
邏輯運行良好,以及append()邏輯。是的,我正在使用教程,並得到了,但至今不得不問我的問題。從我可以告訴的是,這個教程非常好。但是,它目前只能得到一個人(即 - 你的邏輯沒有被覆蓋)。再次,非常感謝! –

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'] 

注意的元素(值),另外的邏輯。

+2

不要使用元組序列創建文字詞典,其語法無需冗長。 –

+0

現在我已經花了一些時間了,我絕對同意你不使用元組序列的立場。但是,[Python詞典教程] [教程]:http://docs.python.org/2/tutorial/datastructures.html#dictionaries以元組爲例。只是在未來爲其他人提供墊腳石。 –

+1

您可以使用:'{'one':1,'two':2,'three':3}'或者如果所有的鍵都是有效的Python標識符:'dict(one = 1,two = 2,three = 3) '。 – jfs