2015-04-03 41 views
-1

我想建立列表作爲值,其中包含類型的字典與列表爲值的字典:使用列表嵌套日文N3 N4 N5

xml_dict = { 
'Spain':['La Palma':[2929, ..], 'Fuerteventura':[5733, ..]], 
'Turkey':['Antalya':[16483, ..], 'Izmir':[2927, ...]] 
... } 

我的出發基礎是有類型的字典列表:

self.db_data_list = [ 
{'land': 'Spain', 'giatahotelcode': 2929, 'zielgebiet_abweichung': 'La Palma'}, 
{'land': 'Spain', 'giatahotelcode': 5733, 'zielgebiet_abweichung': 'Fuerteventura'}, 
{'land': 'Turkey', 'giatahotelcode': 16483, 'zielgebiet_abweichung': 'Antalya'}, 
{'land': 'Turkey', 'giatahotelcode': 2927, 'zielgebiet_abweichung': 'Izmir'} 

而且這是我到目前爲止的代碼:

zg_giata_dict = dict() 
country_zg_dict = dict() 
xml_dict = dict() 

countries = [ value for data_dict in self.db_data_list for key, value in data_dict.iteritems() if key == 'land' ] 
countries = set(countries) 

zgs = [ value for data_dict in self.db_data_list for key, value in data_dict.iteritems() if key == 'zielgebiet_abweichung' ] 
zgs = set(zgs) 

for data_dict in self.db_data_list: 
    for country in countries: 
     if data_dict['land'] == country: 
      country_zg_dict.setdefault(country, []).append(data_dict['zielgebiet_abweichung']) 
      country_zg_dict[country] = list(set(country_zg_dict[country])) 

for data_dict in self.db_data_list: 
    for zg in zgs: 
     if data_dict['zielgebiet_abweichung'] == zg: 
      zg_giata_dict.setdefault(zg, []).append(data_dict['giatahotelcode']) 

for country, zg_list in country_zg_dict.iteritems(): 
    for zg, giata_list in zg_giata_dict.iteritems(): 
     if zg in zg_list: 
      xml_dict.setdefault(country, []).append(giata_list) 

輸出xml_dict:

{'Spain': [[2929, ...], [5733, ...]], 'Turkey': [[16483, ...], [2927, ...]], ... } 

我的輸出結果並不差 - 但我錯過了zielgebiet_abweichung(self.db_data_list)在我的xml_dict中的值。我不知道如何管理它。有任何想法嗎?

+0

你想要的輸出 - '{ '西班牙':'拉帕爾馬':[2929,..],'Fuerteventura':[5733,..]], 'Turkey':['Antalya':[16483,..],'Izmir':[2927,...]] ...}' - 不是有效的字典或列表 – Stuart 2015-04-03 13:33:32

+0

Sorr y',我的錯:'xml_dict ='Spain':[{'La Palma':[2929,..}],'Fuerteventura':[{5733,..]}], '土耳其':[{ '安塔利亞':[16483,..],'伊茲密爾':[2927,...]}] ...}' – jwi 2015-04-03 13:37:55

+0

不,那也行不通。不想要你想要的只是一個嵌套字典,如'{'西班牙':{'拉帕爾馬':[2929,...],'富埃特文圖':[5733,...]},'土耳其':{ 'Antalya':[16483,...],'Izmir':[2927,...]}}' – Stuart 2015-04-03 13:43:46

回答

1

我認爲你實際需要的是一個字典作爲值的字典,其中這些內部字典有列表作爲值。

for hotel in self.db_data_list: 
    land = hotel['land'] 
    town = hotel['zielgebiet_abweichung'] 
    code = hotel['giatahotelcode'] 
    if land in all_data: 
     if town in all_data[land]: 
      all_data[land][town].append(code) 
     else: 
      all_data[land][town] = [code] 
    else: 
     all_data[land] = {town: [code]} 

這將返回:

{'Turkey': {'Antalya': [16483], 'Izmir': [2927]}, 
'Spain': {'Fuerteventura': [5733], 'La Palma': [2929]}} 

您還可以使用setdefault做同樣的事情:

all_data = {} 
for hotel in self.db_data_list: 
    land = all_data.setdefault(hotel['land'], {}) 
    town = land.setdefault(hotel['zielgebiet_abweichung'], []) 
    town.append(hotel['giatahotelcode']) 
+0

Hmpf,這工作正常..但我不明白:'town = land.setdefault(hotel [ 'zielgebiet_abweichung'],[])'在我看來,這條線會覆蓋陸地字典的關鍵。你能解釋這條線嗎?例如:當我在添加代碼之前打印土地或城鎮時,它們只是一個空字典和一個列表。非常感謝你順便說一句。 – jwi 2015-04-03 16:42:04

+1

'list.setdefault(key,default)'返回與'key'相關的值,如果它在字典中,或者如果不創建該鍵並將其值設置爲'default'。 https://docs.python.org/2/library/stdtypes.html#dict.setdefault – Stuart 2015-04-03 17:56:28