[Python] Having itemgetter and attrgetter at the same time

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to pick a most common item among a list based on the element's attribute.

    a1 = Myclass(name='a')
    a2 = Myclass(name='a')
    b1 = Myclass(name='b')
    l = [a1,a2,b1]

    if most_common(l, attr_name='name') in [a1, a2]:
    # I want this true
    # 'a' is the most occured 'name'


    Basically I want to modify the code https://stackoverflow.com/a/1520716/433570

    key = operator.itemgetter(0) then operator.attrgetter(attr_name)

    wonder if that's possible?

    Continue reading...

Compartilhe esta Página