Есть идеи, почему python3 не рассматривает False как bool? - PullRequest
0 голосов
/ 19 апреля 2020

Есть идеи, почему python3 не рассматривает False как bool? Я хочу переместить все нули в конец списка.

def move_zeros(array):

    for i in array:
        if type(i) is not bool:
            if i == 0:
                array.append(int(i)) # catching non bool values that are zeros, adding at the end of the list
                array.remove(i) # removing original
        elif type(i) is bool:
            pass #Here it catches False from the input, it should do nothing but somehow it is moved to the end of the list as zero in the output.

    return array


print(move_zeros(["a", 0, 0, "b", None, "c", "d", 0, 1,
                  False, 0, 1, 0, 3, [], 0, 1, 9, 0, 0, {}, 0, 0, 9]))

Вывод:

['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

1 Ответ

1 голос
/ 19 апреля 2020

Попробуйте эту небольшую модификацию вашего кода:

def move_zeros(array):
    new_list = []   # Work a new list
    for i in array:
        print(f"I = {i}")
        if type(i) is not bool:
            if i == 0:
                new_list.append(int(i))
        else:
            pass

    return new_list


print(move_zeros(["a", 0, 0, "b", None, "c", "d", 0, 1,
                  False, 0, 1, 0, 3, [], 0, 1, 9, 0, 0, {}, 0, 0, 9]))
...