Python—如何通过键和返回值将列表与字典进行比较—然后放入列表中

Python—如何通过键和返回值将列表与字典进行比较—然后放入列表中,python,list,Python,List,我举一个例子: birthday_persons = ['1966-06-26T11:50:25.558Z', '1949-10-09T00:25:51.304Z'] dates_ids = {'1966-06-26T11:50:25.558Z': 1, '1949-10-09T00:25:51.304Z': 2, '1992-11-21T06:28:32.563Z': 3} Dict key是出生日期,Dict value是一个id号 我需要比较列表和dict键,若列表中的元素相等,则返回

我举一个例子:

birthday_persons = ['1966-06-26T11:50:25.558Z', '1949-10-09T00:25:51.304Z']

dates_ids = {'1966-06-26T11:50:25.558Z': 1, '1949-10-09T00:25:51.304Z': 2, '1992-11-21T06:28:32.563Z': 3}
Dict key是出生日期,Dict value是一个id号

我需要比较列表和dict键,若列表中的元素相等,则返回dict.value(id)

我该怎么做呢?

基本上是这样的:

for birthday_person in birthday_persons:
    if birthday_person in dates_ids:
        value = dates_ids.get(birthday_person)
        print(value)

您检查dict中是否存在此人,然后获取值,您只需使用for循环即可将其存档

for bday in birthday_persons:
  if bday in dates_ids.keys():
    return dates_ids[bday]
对于生日为b的人:
打印(日期\u ID[b日期])

使用列表理解,
[dates\u id[x]代表生日中的x\u人如果是日期\u id.get(x)]
谢谢,太好了!或者使用类似上述表格@Sushanth的列表理解