1

I have a list of tuples as follows:

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

I need to extract tuples in which number 1 is repeated less than 5 times. I have read here, here and some other posts, and based on these I have written the following:

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)

This is not working giving me another list of lists. Could anyone give me a hint what I am doing wrong here? Thanks!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
RezAm
  • 548
  • 2
  • 6
  • 20

3 Answers3

1

A shorter solution can utilize a list comprehension with 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]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

using filter is a good option.

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)

output

[(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)]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
1

You were very close. As usual with Python, things are simpler than we think:

 result = [t for t in my_list if t.count(1) < 5]
Alain T.
  • 40,517
  • 4
  • 31
  • 51