我想要查询列表中嵌套的列表的特定元素是否含有该怎么做
eg:[["X","O"],["","X"]] 判断该列表中是否含有“"
我想要查询列表中嵌套的列表的特定元素是否含有该怎么做eg:[["X","O"],["","X"]] 判断该列表中是否含有“"
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
|__WhoAmI__| 2022-12-13 12:03关注如果你想判断嵌套列表中是否包含特定的元素,那么你可以使用 Python 的 in 操作符,例如:
nested_list = [["X","O"],["","X"]] if "" in nested_list: print("The nested list contains an empty string.")注意,如果你想判断一个嵌套列表中是否包含另一个列表,那么你需要使用一个循环来枚举嵌套列表的每个元素,并判断这些元素是否与你要查找的列表相同:
nested_list = [["X","O"],["","X"]] search_list = ["","X"] found = False for element in nested_list: if element == search_list: found = True break if found: print("The nested list contains the search list.") else: print("The nested list does not contain the search list.")上面的代码会在嵌套列表中找到第二个列表时输出“The nested list contains the search list.”。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报