温馨提示×

map如何判断是否有某一个key

MAP
小亿
85
2024-03-27 16:08:33
栏目: 编程语言

在python中,可以使用in关键字来判断一个key是否存在于map中。示例代码如下:

my_map = {'a': 1, 'b': 2, 'c': 3}

if 'a' in my_map:
    print('Key "a" exists in the map.')
else:
    print('Key "a" does not exist in the map.')

以上代码将输出Key "a" exists in the map.,因为map中存在key为'a'的键。如果要判断一个key是否不存在于map中,可以使用not in关键字。

0