从列表中有条件地选择元组

时间:2019-05-24 22:06:41

标签: python list tuples

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

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)]

我需要提取重复少于5次的数字1的元组。我已经阅读了herehere和其他一些文章,并根据这些文章撰写了以下文章:

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 个答案:

答案 0 :(得分:1)

一个简短的解决方案可以利用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]

答案 1 :(得分:1)

使用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)]

答案 2 :(得分:1)

您非常亲密。和Python一样,事情比我们想象的要简单:

 result = [t for t in my_list if t.count(1) < 5]