【问题标题】:Search in sub sets of set which are lists [duplicate]在列表的子集中搜索[重复]
【发布时间】:2019-01-10 11:41:30
【问题描述】:

我得到了:

list = [{'a','b'},{'c','d'}]

我想知道“a”是否在列表中。

我应该先将列表作为here 解压到new_list 并在new_list 中使用'a'

或者有没有更短的方法(不导入模块)

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    使用any

    spam = [{'a','b'},{'c','d'}]
    eggs = [{'d','b'},{'c','d'}]
    
    print(any('a' in my_set for my_set in spam))
    print(any('a' in my_set for my_set in eggs))
    

    输出

    True
    False
    >>>
    

    【讨论】:

      【解决方案2】:

      这是使用any 的一种方法。

      例如:

      l = [{'a','b'},{'c','d'}]
      print( any(map(lambda x: "a" in x, l)) )
      

      输出:

      True
      

      【讨论】:

        【解决方案3】:

        希望能解决。我把它写在一个函数上使用“return”关键字

        def a_is_there():
        
            lists = [{'a','b'},{'c','d'}]
            for list in lists:
                if "a" in list:
                    print("True")
                    return True
            print("False")
            return False
        
        
        a_is_there()
        

        谢谢

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-22
          • 2019-05-25
          • 1970-01-01
          • 2014-04-21
          • 2013-01-30
          • 1970-01-01
          相关资源
          最近更新 更多