可以使用in关键字来判断map的key是否包含在list中的一个值。示例如下:

my_map = {'key1': 1, 'key2': 2, 'key3': 3}
my_list = ['key1', 'key2', 'key4']

for key in my_list:
    if key in my_map:
        print(f"{key} is in the map")
    else:
        print(f"{key} is not in the map")

输出结果为:

key1 is in the map
key2 is in the map
key4 is not in the map
map的key判断是否包含list中的一个值

原文地址: http://www.cveoy.top/t/topic/h8g2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录