[Python] How to filter through a defaultdict?

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a CSV input file that I organized as a defaultdict:

    {
    '1234' : [('1,60', 'text', 'supplier1')],
    '3456' : [('1,98', 'another text', 'supplier2')],
    ['1,54', 'text again', 'supplier1'],
    '709' : [('2,90', 'again', 'supplier2')]
    }


    I would like to is prioritize tuples containing "supplier1". If there are results for the same key for supplier1 and 2, keep only supplier1. And if no results for a given key for supplier1, keeping results with other suppliers.

    EDIT : desired output :

    {
    '1234' : [('1,60', 'text', 'supplier1')],
    '3456' : ['1,54', 'text again', 'supplier1'],
    '709' : [('2,90', 'again', 'supplier2')]
    }

    Continue reading...

Compartilhe esta Página