[Python] Creating an empty set [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have some code which tots up a set of selected values. I would like to define an empty set and add to it, but {} keeps turning into a dictionary. I have found if I populate the set with a dummy value I can use it, but it's not very elegant. Can someone tell me the proper way to do this? Thanks.

    inversIndex = {'five': {1}, 'ten': {2}, 'twenty': {3},
    'two': {0, 1, 2}, 'eight': {2}, 'four': {1},
    'six': {1}, 'seven': {1}, 'three': {0, 2},
    'nine': {2}, 'twelve': {2}, 'zero': {0, 1, 3},
    'eleven': {2}, 'one': {0}}

    query = ['four', 'two', 'three']

    def orSearch(inverseIndex, query):
    b = [ inverseIndex[c] for c in query ]
    x = {'dummy'}
    for y in b:
    { x.add(z) for z in y }
    x.remove('dummy')
    return x

    orSearch(inverseIndex, query)


    {0, 1, 2}
    ​

    Continue reading...

Compartilhe esta Página