Python-如何通过键比较列表和字典并返回值-然后放入列表

时间:2020-08-09 14:08:22

标签: 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}

字典键是生日,字典值是ID号。

如果列表中的元素相等,我需要比较list和dict键并返回dict.value(id)。

我该怎么做?

3 个答案:

答案 0 :(得分:0)

基本上是这样的:

        const room= await RoomModel.findOne({_id: "5f2fde82b64b4e1c787ea611"})
        await new ParticipantsModel({"group_id": room.group_id}).save()

        const newGroupId= utils.randomString(16)
        const filled =await ParticipantsModel.countDocuments({group_id: room.group_id})
        console.log("filled "+filled)
        await RoomModel.updateOne({total_participants:filled},{group_id: newGroupId}).exec()

您检查字典中是否存在该人,然后获取值

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以简单地使用for循环将其存档

matplotlib.use()
相关问题