且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查项目是否在数组/列表中

更新时间:2022-04-17 20:04:42

假设您说列表"的意思是数组",则可以

Assuming you mean "list" where you say "array", you can do

if item in my_list:
    # whatever

这适用于所有集合,而不仅限于列表.对于字典,它将检查字典中是否存在给定的键.

This works for any collection, not just for lists. For dictionaries, it checks whether the given key is present in the dictionary.