猿问

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

我有一个例子:

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.value (id)。

我怎样才能做到这一点?


翻过高山走不出你
浏览 121回答 3
3回答

慕雪6442864

你可以简单地使用 for 循环来归档这个for bday in birthday_persons:     print(dates_ids[bday])

慕尼黑5688855

for bday in birthday_persons:   if bday in dates_ids.keys():       return dates_ids[bday]

慕勒3428872

它基本上是:for birthday_person in birthday_persons:     if birthday_person in dates_ids:         value = dates_ids.get(birthday_person)                 print(value)您检查该人是否存在于字典中,然后获取该值
随时随地看视频慕课网APP

相关分类

Python
我要回答