2015-06-10 7 views
5

Ho un dizionario, che le chiavi sono numeri interi. Ho arbitrariamente cambiato una delle chiavi in ​​una data e ho bisogno di cambiare le altre chiavi.Modifica più chiavi dal dizionario, mentre si esegue l'operazione timedelta in Python

dati del campione:

{'C-STD-B&M-SUM': {datetime.date(2015, 7, 12): 0, 
       -1: 0.21484699999999998, 
       -2: 0.245074, 
       -3: 0.27874} 

uscita prevista:

{'C-STD-B&M-SUM': {datetime.date(2015, 7, 12): 0, 
       datetime.date(2015, 7, 11): 0.21484699999999998, 
       datetime.date(2015, 7, 10): 0.245074, 
       datetime.date(2015, 7, 9): 0.27874} 

codice attuale finora:

def change_start_date(dictionary_with_temporal_distribution): 
    unsw_mid_year_end_date = datetime.date(2015, 7, 12) 
    dictionary_with_temporal_distribution['C-STD-B&M-SUM'][unsw_mid_year_end_date] = dictionary_with_temporal_distribution['C-STD-B&M-SUM'][0] 
    del dictionary_with_temporal_distribution['C-STD-B&M-SUM'][0] 
    for k, v in dictionary_with_temporal_distribution['C-STD-B&M-SUM'].items(): 
+0

Avete bisogno del - nell'output atteso come bene? '-datetime.date()'? –

+0

No, l'ho appena modificato! –

risposta

1

Si può provare qualcosa di simile -

def change_start_date(dictionary_with_temporal_distribution): 
    unsw_mid_year_end_date = datetime.date(2015, 7, 12) 
    for k in list(dictionary_with_temporal_distribution['C-STD-B&M-SUM'].keys()): 
     dictionary_with_temporal_distribution['C-STD-B&M-SUM'][unsw_mid_year_end_date + timedelta(days=k)] = dictionary_with_temporal_distribution['C-STD-B&M-SUM'][k] 
     del dictionary_with_temporal_distribution['C-STD-B&M-SUM'][k] 
+0

Ho capito: TipoErrore: tipo non supportato per componente giorni timedelta: datetime.date –

+0

assicurati che tutte le chiavi siano integer prima di eseguire il ciclo. non modificare l'elemento '0'th prima del ciclo –

+0

Risolto il problema, ora ho ottenuto: RuntimeError: dizionario cambiato dimensione durante l'iterazione –

0

È possibile utilizzare la sintassi comprensione dict e trasformare e sostituire le chiavi:

dct = {datetime.date(2015, 7, 12): 0, 
    -1: 0.21484699999999998, 
    -2: 0.245074, 
    -3: 0.27874} 

def offset(offset, base): 
    """Applies an offset in days to a base date. 
     If the offset is already a date it is returned as is.""" 
    if type(offset) == datetime.date: 
     return offset 
    return base + datetime.timedelta(offset) 

def offset_keys(dct, base): 
    """Takes a dict and runs offset(key, base) on all keys""" 
    return { offset(k, base): v for k, v in dct.items() } 

pprint(offset_keys(dct, datetime.date(2015, 7, 12))) 
{datetime.date(2015, 7, 9): 0.27874, 
datetime.date(2015, 7, 10): 0.245074, 
datetime.date(2015, 7, 11): 0.21484699999999998, 
datetime.date(2015, 7, 12): 0} 
+0

Ho capito: TipoErrore: tipo non supportato per componente giorni timedelta: str –

+0

Quindi non stai passando il dizionario corretto al metodo. Assicurati di passare quello con 'datetime.date' e gli interi come chiavi e non quello con' "C-STD-B & M-SUM" 'come è solo la chiave – Raniz

Problemi correlati