[Python] Accessing the list while being sorted

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    Can I access a list while it is being sorted in the list.sort()

    b = ['b', 'e', 'f', 'd', 'c', 'g', 'a']
    f = 'check this'

    def m(i):
    print i, b, f
    return None

    b.sort(key=m)
    print b


    this returns

    b [] check this
    e [] check this
    f [] check this
    d [] check this
    c [] check this
    g [] check this
    a [] check this


    Note that individual items of list b is sent to function m. But at m the list b is empty, however it can see the variable f, which has same scope as list b. Why does function m print b as []?

    Continue reading...

Compartilhe esta Página