python中判断字典中是否包含特定键

 

>>> test1 = {"key1":"aa","key2":"bb","key3":"cc","key4":"dd","key5":"ee","key6":"ff","key7":"gg"}
>>> type(test1)
<class 'dict'>
>>> len(test1)
7
>>> keys = ["key6","key10","key3","key8","key20","key1"]
>>> for i in keys:
    if i in test1:
        print(i,"in")
    else:
        print(i,"not in")

        
key6 in
key10 not in
key3 in
key8 not in
key20 not in
key1 in

 

posted @ 2020-12-31 21:07  小鲨鱼2018  阅读(3338)  评论(0编辑  收藏  举报