h08.14 2022-12-13 11:50 采纳率: 50%
浏览 12
已结题

我想要查询列表中嵌套的列表的特定元素是否含有该怎么做eg:[["X","O"],["","X"]] 判断该列表中是否含有“"

我想要查询列表中嵌套的列表的特定元素是否含有该怎么做
eg:[["X","O"],["","X"]] 判断该列表中是否含有“"

  • 写回答

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.”。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 创建了问题 12月13日