2016-10-19 72 views
0

我有一個defaultdict,我想創建一個字典作爲值的一部分,我可以追加到這個字典嗎?目前我正在追加一份名單,這意味着詞典是分開的。追加到默認字典中的字典

dates = [datetime.date(2016, 10, 17), datetime.date(2016, 10, 18), datetime.date(2016, 10, 19), datetime.date(2016, 10, 20), datetime.date(2016, 10, 21), datetime.date(2016, 10, 22), datetime.date(2016, 10, 23)] 

e = defaultdict(list) 
for key, value in d.iteritems(): 
    value = (sorted(value, key=itemgetter('date'), reverse=False)) 
    for date in dates: 
     for i in value: 
      if i['date'] == str(date) and i['time'] == 'morning': 
       value1 = float(i['value1']) 
       temp = {'val_morning': value1 } 
       e[str(date)].append(temp) 
      elif ii['date'] == str(date) and i['time'] == 'evening': 
       value2 = float(i['value2']) 
       temp = {'val_evening': value2 } 
       e[str(date)].append(temp) 

導致:

{'2016-10-20': [{'val_morning': 0.0}, {'val_evening': 0.0}], '2016-10-21': [{'val_morning': 0.0}, {'val_evening': 0.0}]} 

編輯 所需的輸出:

{       
    '2016-10-20': {'val_morning': 0.0, 'val_evening': 0.0}, 
    '2016-10-21': {'val_morning': 0.0, 'val_evening': 0.0} 
} 
+0

爲什麼你想要的輸出包含一個只有一個項目的列表?看起來這個列表是不必要的 - 不會'{'2016-10-20':{'value1':0.0,'value2':0.0},'2016-10-21':...'be better ? – mgilson

+0

表示同意,報廢清單。將更新問題。謝謝 – user3939059

+0

每天總是隻有2個值(早上對應於'value1',對應於'value2'的晚上對應一個)?或者一天能有更多的價值?在這種情況下你想做什麼? – mgilson

回答

0

如果我理解正確的話,你想用字典來代替列表,你可以稍後添加到它的值。

如果是的話,你可以這樣做:

dates = [datetime.date(2016, 10, 17), datetime.date(2016, 10, 18), datetime.date(2016, 10, 19), datetime.date(2016, 10, 20), datetime.date(2016, 10, 21), datetime.date(2016, 10, 22), datetime.date(2016, 10, 23)] 

e = defaultdict(dict) 
for key, value in d.iteritems(): 
    value = (sorted(value, key=itemgetter('date'), reverse=False)) 
    for date in dates: 
     for i in value: 
      if i['date'] == str(date) and i['time'] == 'morning': 
       value1 = float(i['value1']) 
       temp = {'val_morning': value1 } 
       e[str(date)].update(temp) #### HERE i replaced append with update! 
      elif ii['date'] == str(date) and i['time'] == 'evening': 
       value2 = float(i['value2']) 
       temp = {'val_evening': value2 } 
       e[str(date)].update(temp)#### HERE i replaced append with update! 

我簡單地更換追加與update(當然取得了defaultdict使用字典而不是列表)

0

如果我理解正確

import datetime 
dates = [datetime.date(2016, 10, 17), datetime.date(2016, 10, 18), datetime.date(2016, 10, 19), datetime.date(2016, 10, 20), datetime.date(2016, 10, 21), datetime.date(2016, 10, 22), datetime.date(2016, 10, 23)] 

dict_x = {} 
for i in map(str,set(dates)): 
    dict_x[i] = {'val_morning': 0.0, 'val_evening': 0.0} 

dict_x 

output : 
{'2016-10-17': {'val_evening': 0.0, 'val_morning': 0.0}, 
'2016-10-18': {'val_evening': 0.0, 'val_morning': 0.0}, 
'2016-10-19': {'val_evening': 0.0, 'val_morning': 0.0}, 
'2016-10-20': {'val_evening': 0.0, 'val_morning': 0.0}, 
'2016-10-21': {'val_evening': 0.0, 'val_morning': 0.0}, 
'2016-10-22': {'val_evening': 0.0, 'val_morning': 0.0}, 
'2016-10-23': {'val_evening': 0.0, 'val_morning': 0.0}}