[Python] Identify the list that is the odd one out, and find its index from given lists

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I am given a list of lists that contains exactly one list that is different from the others.

    I tried to solve that with this code:

    a = [[1, -2], [1, -2], [1,6], [1, -2], [1, -2]]

    def find_idx(lists):
    for i in range(len(lists)-1):
    diff = np.array(lists[0])-np.array(lists[i+1])
    for k in diff:
    if k != 0:
    print(i+1)
    find_idx(a)


    The above function works well for most inputs, but when the odd one out is at index 0, it returns the wrong result.

    For example, with this input:

    (lists = [[1, 6],[1, -2], [1, -2], [1, -2], [1, -2]])


    I get all indices printed, except the one that I really should get (0).

    How can I fix my function so it also works when the differing list sits at index 0?

    Continue reading...

Compartilhe esta Página