代码之家  ›  专栏  ›  技术社区  ›  RezAm

从列表中条件选择元组

  •  1
  • RezAm  · 技术社区  · 5 年前

    我有一个元组列表,如下所示:

    my_list = [(3, 3, 3, 3, 3), (1, 2, 3, 3, 3, 3), (2, 2, 2, 3, 3, 3), (1, 1, 1, 3, 3, 3, 3), (1, 1, 2, 2, 3, 3, 3), (1, 2, 2, 2, 2, 3, 3), (2, 2, 2, 2, 2, 2, 3), (1, 1, 1, 1, 2, 3, 3, 3), (1, 1, 1, 2, 2, 2, 3, 3), (1, 1, 2, 2, 2, 2, 2, 3), (1, 2, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 3, 3, 3), (1, 1, 1, 1, 1, 2, 2, 3, 3), (1, 1, 1, 1, 2, 2, 2, 2, 3), (1, 1, 1, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 2, 3, 3), (1, 1, 1, 1, 1, 1, 2, 2, 2, 3), (1, 1, 1, 1, 1, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3), (1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3), (1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)]
    

    我需要提取哪个数的元组 1 重复次数少于5次。我读过 here , here 以及其他一些帖子,基于这些,我写了如下:

    results = []
    for i in range(len(my_list)):           
        a = [elem for elem in my_list if my_list[i].count(1) < 5]
        results.append(a)
    

    再给我一张单子就不行了。有人能告诉我我做错了什么吗?谢谢!

    3 回复  |  直到 5 年前
        1
  •  1
  •   Alain T.    5 年前

    你很亲密。与Python一样,事情比我们想象的要简单:

     result = [t for t in my_list if t.count(1) < 5]
    
        2
  •  1
  •   Ajax1234    5 年前

    较短的解决方案可以利用列表理解 sum :

    my_list = [(3, 3, 3, 3, 3), (1, 2, 3, 3, 3, 3), (2, 2, 2, 3, 3, 3), (1, 1, 1, 3, 3, 3, 3), (1, 1, 2, 2, 3, 3, 3), (1, 2, 2, 2, 2, 3, 3), (2, 2, 2, 2, 2, 2, 3), (1, 1, 1, 1, 2, 3, 3, 3), (1, 1, 1, 2, 2, 2, 3, 3), (1, 1, 2, 2, 2, 2, 2, 3), (1, 2, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 3, 3, 3), (1, 1, 1, 1, 1, 2, 2, 3, 3), (1, 1, 1, 1, 2, 2, 2, 2, 3), (1, 1, 1, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 2, 3, 3), (1, 1, 1, 1, 1, 1, 2, 2, 2, 3), (1, 1, 1, 1, 1, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3), (1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3), (1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)]
    result = [i for i in my_list if sum(c == 1 for c in i) < 5]
    
        3
  •  1
  •   sahasrara62    5 年前

    使用 filter 是个不错的选择。

    my_list = [(3, 3, 3, 3, 3), (1, 2, 3, 3, 3, 3), (2, 2, 2, 3, 3, 3), (1, 1, 1, 3, 3, 3, 3), (1, 1, 2, 2, 3, 3, 3), (1, 2, 2, 2, 2, 3, 3), (2, 2, 2, 2, 2, 2, 3), (1, 1, 1, 1, 2, 3, 3, 3), (1, 1, 1, 2, 2, 2, 3, 3), (1, 1, 2, 2, 2, 2, 2, 3), (1, 2, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 3, 3, 3), (1, 1, 1, 1, 1, 2, 2, 3, 3), (1, 1, 1, 1, 2, 2, 2, 2, 3), (1, 1, 1, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 2, 3, 3), (1, 1, 1, 1, 1, 1, 2, 2, 2, 3), (1, 1, 1, 1, 1, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3), (1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3), (1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)]
    
    
    sol =list(filter (lambda x:x.count(1)<5, my_list))
    print(sol)
    

    输出

    [(3, 3, 3, 3, 3),
     (1, 2, 3, 3, 3, 3),
     (2, 2, 2, 3, 3, 3),
     (1, 1, 1, 3, 3, 3, 3),
     (1, 1, 2, 2, 3, 3, 3),
     (1, 2, 2, 2, 2, 3, 3),
     (2, 2, 2, 2, 2, 2, 3),
     (1, 1, 1, 1, 2, 3, 3, 3),
     (1, 1, 1, 2, 2, 2, 3, 3),
     (1, 1, 2, 2, 2, 2, 2, 3),
     (1, 2, 2, 2, 2, 2, 2, 2),
     (1, 1, 1, 1, 2, 2, 2, 2, 3),
     (1, 1, 1, 2, 2, 2, 2, 2, 2)]