1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Iterating through dict and list2d, list_value gets replaced with a str

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I am iterating through a dict and after that through a 2d list[list[float|int] then i am changing the values of the list2d to the corresponding in the dict randomly a str appears

    from the test i did it changes based on the size of the list2d example:

    50, 50 = 13, 49

    80, 80 = 15, 48


    terrain_probability_dict: {float | int, str} = {-1: "e", 0.2 : "o", 0.5 : "M", 1 : "#"}
    def generate_terrain(world: list[list[float | int]]):

    keys = sorted(terrain_probability_dict.keys())
    for i in range(1, len(keys)):
    for y in range(len(world)):
    for x in range(len(world[0])):
    if keys[i - 1] < world[y][x] <= keys:
    world[y][x] = terrain_probability_dict[keys]


    how i checked it:


    for y in range(len(world)):
    for x in range(len(world[0])):
    # Print only if the type of the current value is string

    if isinstance(world[y][x], str):
    print(f"Initial value at world[{y}][{x}]: {world[y][x]} (Type: {type(world[y][x])})")
    #and
    print(y, x)


    Before the iteration and also the if isinstance code in the iteration From the prints i got that there is a "M" or "o" in the list when iterating

    Why does a index in world change to int in the world

    the error after about 15*50 iteration(depending on the list size):


    if keys[i - 1] < current_value <= keys:


    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: '<' not supported between instances of 'float' and 'str'

    Continue reading...

Compartilhe esta Página