Python string if judgment problem

`{"_type": "video", "extraction_tags": "", "user_id": "2077093354", "highlight": None, "user_info": "{"id":2077093354,"avatar_url":"https:\\/\\/krplus-pic.b0.upaiyun.com\\/avatar\\/201806\\/05084648\\/f1ggwy0wqraluwdz","name":"\\u8d22\\u7ea6\\u4f60","nickname":"\\u8d22\\u7ea6\\u4f60"}", "project_id": "1", "vtype": "normal", "published_at": "2018-11-13T14:31:09+08:00", "tag_id": "0", "_score": None, "column_id": "213", "title": ":", "cover": "https://pic.36krcnd.com/201811/12082329/jhu9pf7ggeosdbft!heading", "template_info": {"template_extra": {"vtype": "normal"}, "template_type": "small_image", "template_title": ":", "template_title_isSame": True, "template_cover": ["https://pic.36krcnd.com/201811/12082329/jhu9pf7ggeosdbft!heading"]}, "web_cover": "https://pic.36krcnd.com/201811/12082256/sekti37ljzprt2hj", "column_name": "", "id": "24250", "summary": ""`" "}

< class" dict" >
there is video in this dictionary. Can"t I make a direct judgment like this?

if "video" not in (content):
         title_list.append(content["template_info"]["template_title"])
         time_list.append(content["published_at"])
         link_list.append("https://36kr.com/p/{}.html".format(content["id"]))
         

my content cannot always be judged without str. If you add str, you can judge
. Why?

Nov.11,2021

because the use of in in str is different from that of in in dictionaries.

  • you add str (content) to convert the dictionary into a string, and it is obvious that there is a 'video',' in the content string with in.
  • if you use in, in the dictionary only to determine whether key is in dict, and 'video' is the value of the dictionary, so the result is empty.
For the specific usage of

, see the following example

In [1]: dict_demo = {"h":"hello","w":"world"}

In [2]: if "hello" in dict_demo:
   ...:     print("hello yes")
   ...:     

In [3]: if "h" in dict_demo:
   ...:     print("h yes")
   ...:     
h yes

In [4]: if "hello" in str(dict_demo):
   ...:     print("hello in string")
   ...:     
hello in string

if you want to determine whether a value exists, you can use the dict.values () method, as follows

In [5]: if "hello" in dict_demo.values():
   ...:     print("hello in values")
   ...:     
hello in values
Menu