2016-03-01 53 views
1

我有日期設置像這樣的列表:用字典和輸出真假對比清單

listdates = [datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25), datetime.date(2016, 4, 26), datetime.date(2016, 4, 27)] 

我有這樣設置日期的詞典:

dictdates = {'S1': [datetime.date(2016, 4, 20), datetime.date(2016, 4, 21), datetime.date(2016, 4, 22), datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25), datetime.date(2016, 4, 26), datetime.date(2016, 4, 27), datetime.date(2016, 4, 28), datetime.date(2016, 4, 29)], 'W26': [datetime.date(2016, 4, 20), datetime.date(2016, 4, 21), datetime.date(2016, 4, 22), datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25)], 'W24': [datetime.date(2016, 4, 25)]} 

我想比較列表到字典,如果列表中的值與字典中的值匹配,則它應該打印不可用。

我有以下工作:

for val in listdates: 
    if val in dictdates['S1']: 
     print val, 'unavailable' 
    else: 
     print val, 'available' 

這使輸出:

2016-04-23 unavailable 
2016-04-24 unavailable 
2016-04-25 unavailable 
2016-04-26 unavailable 
2016-04-27 unavailable 

我想要什麼,而不是,是每一個列表,請在字典中的和輸出的關鍵可用/不可用的,就像這樣:

S1 
2016-04-23 unavailable 
2016-04-24 unavailable 
2016-04-25 unavailable 
2016-04-26 unavailable 
2016-04-27 unavailable 

W26 
2016-04-23 unavailable 
2016-04-24 unavailable 
2016-04-25 unavailable 
2016-04-26 unavailable 
2016-04-27 unavailable 

W24 
2016-04-23 available 
2016-04-24 available 
2016-04-25 unavailable 
2016-04-26 available 
2016-04-27 available 

etc ... 

回答

1

循環列表和輸出你正在尋找

for key, value in dictdates.items(): 
    print key 
    for val in listdates: 
     if val in value: 
      print val, 'unavailable' 
     else: 
      print val, 'available' 
    print "\n" 
1
import datetime 

listdates = [datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25), datetime.date(2016, 4, 26), datetime.date(2016, 4, 27)] 
dictdates = {'S1': [datetime.date(2016, 4, 20), datetime.date(2016, 4, 21), datetime.date(2016, 4, 22), datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25), datetime.date(2016, 4, 26), datetime.date(2016, 4, 27), datetime.date(2016, 4, 28), datetime.date(2016, 4, 29)], 'W26': [datetime.date(2016, 4, 20), datetime.date(2016, 4, 21), datetime.date(2016, 4, 22), datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25)], 'W24': [datetime.date(2016, 4, 25)]} 

for key in dictdates: 
    print key 
    for val in listdates: 
     if val in dictdates[key]: 
      print val, 'unavailable' 
     else: 
      print val, 'available' 
1

您需要遍歷字典的鍵以及:

for key, dates in dictdates.items(): 
    print key 
    for val in listdates: 
     if val in dates: 
      print val, 'unavailable' 
     else: 
      print val, 'available' 
+0

輕微改善:由於這是的Py2,使用'.iteitems()'或'.viewitems()'(後者僅在2.7中,但更接近Py3的.items()行爲),而不是'.items()',在Py2中,創建一個所有對的「列表」,而不是直接迭代「dict」。 – ShadowRanger

1

如果我正確認識你,只是通過所有的迭代值在字典中通過列表迭代沿日期

當你遍歷一個字典,你遍歷關鍵字。

for i in dictdates: # i is the key in the dictionary 
    print i 
    for val in listdates: 
     if val in dictdates[i]: 
      print val, 'available' 
     else: 
      print val, 'unavailable' 

換句話說,對於字典中的每個鍵,檢查列表日期中的每個日期,並將其與鍵中的值進行比較。

希望這有助於通過字典和這個循環裏

0

創建結果的信息,我使用使用嵌套列表理解OrderedDict在內部字典上保持日期排序。

sorted(list(set(listdates + dictdates[k]))創建詞典日期與listdates中的聯合的有序列表。然後這被用於遍歷每個字典條目的值(例如'S1','W24'等)並檢查日期是否在那裏。

from collections import OrderedDict 

d2 = {k: OrderedDict((d, 'unavailable' if d in dictdates[k] else 'available') 
        for d in sorted(list(set(listdates + dictdates[k])))) 
     for k in dictdates} 
>>> d2 
{'S1': OrderedDict([(datetime.date(2016, 4, 20), 'unavailable'), 
       (datetime.date(2016, 4, 21), 'unavailable'), 
       (datetime.date(2016, 4, 22), 'unavailable'), 
       (datetime.date(2016, 4, 23), 'unavailable'), 
       (datetime.date(2016, 4, 24), 'unavailable'), 
       (datetime.date(2016, 4, 25), 'unavailable'), 
       (datetime.date(2016, 4, 26), 'unavailable'), 
       (datetime.date(2016, 4, 27), 'unavailable'), 
       (datetime.date(2016, 4, 28), 'unavailable'), 
       (datetime.date(2016, 4, 29), 'unavailable')]), 
'W24': OrderedDict([(datetime.date(2016, 4, 23), 'available'), 
       (datetime.date(2016, 4, 24), 'available'), 
       (datetime.date(2016, 4, 25), 'unavailable'), 
       (datetime.date(2016, 4, 26), 'available'), 
       (datetime.date(2016, 4, 27), 'available')]), 
'W26': OrderedDict([(datetime.date(2016, 4, 20), 'unavailable'), 
       (datetime.date(2016, 4, 21), 'unavailable'), 
       (datetime.date(2016, 4, 22), 'unavailable'), 
       (datetime.date(2016, 4, 23), 'unavailable'), 
       (datetime.date(2016, 4, 24), 'unavailable'), 
       (datetime.date(2016, 4, 25), 'unavailable'), 
       (datetime.date(2016, 4, 26), 'available'), 
       (datetime.date(2016, 4, 27), 'available')])} 

要打印結果:

for k, dates in d2.iteritems(): 
    print(k) 
    for d in dates.iteritems(): 
     print("{0}: {1}".format(d[0].strftime("%Y-%m-%d"), d[1])) 
W26 
2016-04-20: unavailable 
2016-04-21: unavailable 
2016-04-22: unavailable 
2016-04-23: unavailable 
2016-04-24: unavailable 
2016-04-25: unavailable 
2016-04-26: available 
2016-04-27: available 
S1 
2016-04-20: unavailable 
2016-04-21: unavailable 
2016-04-22: unavailable 
2016-04-23: unavailable 
2016-04-24: unavailable 
2016-04-25: unavailable 
2016-04-26: unavailable 
2016-04-27: unavailable 
2016-04-28: unavailable 
2016-04-29: unavailable 
W24 
2016-04-23: available 
2016-04-24: available 
2016-04-25: unavailable 
2016-04-26: available 
2016-04-27: available 
0

這是2016年不寫代碼,不python3 :)工作

from __future__ import print_function 
import datetime 

listdates = [datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25), datetime.date(2016, 4, 26), datetime.date(2016, 4, 27)] 
dictdates = {'S1': [datetime.date(2016, 4, 20), datetime.date(2016, 4, 21), datetime.date(2016, 4, 22), datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25), datetime.date(2016, 4, 26), datetime.date(2016, 4, 27), datetime.date(2016, 4, 28), datetime.date(2016, 4, 29)], 'W26': [datetime.date(2016, 4, 20), datetime.date(2016, 4, 21), datetime.date(2016, 4, 22), datetime.date(2016, 4, 23), datetime.date(2016, 4, 24), datetime.date(2016, 4, 25)], 'W24': [datetime.date(2016, 4, 25)]} 

for kk, vv in dictdates.items(): 
    print(kk) 
    for ii in vv: 
     print("{0}: {1}".format(ii, ('unavailable' if ii in listdates else 'available')))