将列表与字典进行比较并输出true和false

时间:2016-03-01 20:26:51

标签: python list dictionary generator

我有一个列表,其中包含以下日期:

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 ...

6 个答案:

答案 0 :(得分: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 :(得分:1)

你也需要遍历字典的键:

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

答案 2 :(得分: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'

换句话说,对于字典中的每个键,检查列表日期中的每个日期,并将其与键中的值进行比较。

希望这有帮助

答案 3 :(得分: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"

答案 4 :(得分: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

答案 5 :(得分: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')))