学堂 学堂 学堂公众号手机端

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

lewis 3年前 (2023-02-07) 阅读数 6 #技术

在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关键字。


版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门