2017-02-19 28 views
-4

在Python我有這種表情的對付那種語法:我不明白,使用字典

alg_error = self.__rec_data[seedict][scanner][CALCULATE][CALCULATE_VALUE] 

alg_error = self.__rec_data[seedict][scanner][CALCULATE][CALCULATE_VALUE] 
alg_comp = self.__rec_data[pers][dict1][DATA][DATA_DICT] 

我完全不明白這多個字典表達總之,它是如何工作的,如果我分配例如

algo_dem_error_path_comp = self.__rec_file_data[pers][scan][DATA][DATA_ERR_PATH_COMP] = "value : 5 " 

的值會在哪裏5去?

+1

在Python字典上尋找一些基本的東西http://stackoverflow.com/questions/16333296/how-do-you-create-nested-dict-in-python – ZdaR

回答

2

你有一個嵌套字典,即一個字典,其中的項目本身也是字典。所以每個鍵都在那個字典中找到相應的項目。

foo = {"A": 1, "B": 2, "C": 3} 
bar = {"A:": 2, "B": 4, "C": 6} 

baz = {"X": foo, "Y": bar} 

print(baz["X"]["A"]) 
0

你在這裏看到的是嵌套字典。例如:

>>> dict1 = {'hello': 'sup'} 

>>> dict2 = {} 
>>> dict2['first_thing'] = dict1 

>>> dict2 
{'first_thing': {'hello': 'sup'}} 

>>> dict2['first_thing']['hello'] 
'sup' 

>>> dict2['first_thing']['new_key'] = 'heman' 

>>> dict2 
{'first_thing': {'new_key': 'heman', 'hello': 'sup'}} 

因此,沒有深入到具體數據結構的細節中,希望能夠澄清一些問題。