Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
Filter a dictionary of lists

I have a dictionary of the form:

{"level": [1, 2, 3],
 "conf": [-1, 1, 2],
 "text": ["here", "hel", "llo"]}

I want to filter the lists to remove every item at index i where an index in the value "conf" is not >0.

So for the above dict, the output should be this:

{"level": [2, 3],
 "conf": [1, 2],
 "text": ["hel", "llo"]}

As the first value of conf was not > 0.

I have tried something like this:

new_dict = {i: [a for a in j if a >= min_conf] for i, j in my_dict.items()}

But that would work just for one key.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

try:

from operator import itemgetter


def filter_dictionary(d):
    positive_indices = [i for i, item in enumerate(d['conf']) if item > 0]
    f = itemgetter(*positive_indices)
    return {k: list(f(v)) for k, v in d.items()}


d = {"level": [1, 2, 3], "conf": [-1, 1, 2], "text": ["-1", "hel", "llo"]}
print(filter_dictionary(d))

output:

{'level': [2, 3], 'conf': [1, 2], 'text': ['hel', 'llo']}

I tried to first see which indices of 'conf' are positive, then with itemgetter I picked those indices from values inside the dictionary.

More compact version + without temporary list using generator expression instead:

def filter_dictionary(d):
    f = itemgetter(*(i for i, item in enumerate(d['conf']) if item > 0))
    return {k: list(f(v)) for k, v in d.items()}
over 4 years ago · Santiago Trujillo Relatório

0

I would keep the indexes of valid elements (those greater than 0) with:

kept_keys = [i for i in range(len(my_dict['conf'])) if my_dict['conf'][i] > 0]

And then you can filter each list checking if the index of a certain element in the list is contained in kept_keys:

{k: list(map(lambda x: x[1], filter(lambda x: x[0] in kept_keys, enumerate(my_dict[k])))) for k in my_dict}

Output:

{'level': [2, 3], 'conf': [1, 2], 'text': ['hel', 'llo']}
over 4 years ago · Santiago Trujillo Relatório

0

Here's a one-liner:

dct = {k: [x for i, x in enumerate(v) if d['conf'][i] > 0] for k, v in d.items()}

Output:

>>> dct
{'level': [2, 3], 'conf': [1, 2], 'text': ['hel', 'llo']}

With sample data:

d = {"level":[1,2,3], "conf":[-1,1,2], "text":["here","hel","llo"]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda