D
D
denislysenko2021-11-22 01:23:41
Python
denislysenko, 2021-11-22 01:23:41

How to write a filter for output to the console based on the passed argument?

There is a list with recurring genres. I need to output a certain amount of each genre to the console,

here is my code, I can’t understand where the error is

genres = ['Comedy','Comedy','Comedy','Comedy','Comedy','Comedy','Comedy', 'Dramma','Dramma','Dramma','Dramma','Dramma','Dramma', 'Action','Action','Action','Action','Action','Action','Action','Action','Action','Action','Action','Action','Action']
N = 3 

    
check = []
COUNT = 0        
def filter_number(genre, N, count):
    if genre not in check:
        check.append(genre)
        count = 1
        return True
    if genre in check and count <= N:
        count += 1 
        return True
    
    
for genre in genres:
    if filter_number(genre, N, COUNT):
        print(genre)


I need this code to output to the console:
Comedy
Comedy
Comedy
Dramma
Dramma
Dramma
Action
Action
Action

That is, if N = 2, then the output in the console should be:
Comedy
Comedy
Dramma
Dramma
Action
Action

How to achieve the correct output to the console?

You need a structure like this:
genres = ['какие-то жанры']
N = # какое-то число

def filter()
    #какой-то код для фильтрации

for genre in genres:
    if filter( genre, N):
        print(genre)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-11-22
@denislysenko

def fucking_filter(arr, max_count, key=lambda n: n):
  filtered = []
  count = {}

  for n in arr:
    k = key(n)
    count[k] = count.get(k, 0) + 1
    if count[k] <= max_count:
      filtered.append(n)

  return filtered


for n in fucking_filter(genres, N):
  print(n)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question