[Python] Translate the task from Python (completely ready) to Typescript (90% ready)

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

  1. Stack

    Stack Membro Participativo

    Sometimes I don't know how to solve a problem in other languages, so I solve it in Python and then translate it into another language.

    This is the incoming data.

    slots = [
    {'qwe': "qwevrtyu", 'asd': "hdf dfgfg", 'zxc': "fjfjwvb"},
    {'qwe': "qxcvtyu", 'asd': "hdf mkvfg", 'zxc': "kdkd[vb"},
    {'qwe': "bnmrtyu", 'asd': "hdf hskfyweee", 'zxc': "jfjfjfjfb"},
    {'qwe': "zxcvtyu", 'asd': "ahfh fhfhf", 'zxc': "lflflflf"},
    {'qwe': "qwerbnm", 'asd': "ahfh sgfdhfovppd", 'zxc': "lflflflffff"},
    {'qwe': "qwnm", 'asd': "ahfh hfovppd", 'zxc': "lfdddfff"},
    {'qwe': "qwerbnm", 'asd': "sgfd dessppd", 'zxc': "lflflflffff"},
    {'qwe': "qwerbnm", 'asd': "sgfd qweerpd", 'zxc': "lflflflffff"},
    {'qwe': "qwerbnm", 'asd': "sgfd kfkfkfd", 'zxc': "lflflflffff"}
    ]


    Python code works correctly.


    def processSlots(data):
    result = []
    keys_list = []
    for slot in data:
    elem_key = slot['asd'].split(" ")[0]
    if elem_key in keys_list:
    for my_dict in result:
    try:
    my_dict[elem_key].append(slot)
    except KeyError:
    pass
    else:
    my_dict = {}
    elem_value = []
    my_dict[elem_key] = elem_value
    elem_value.append(slot)
    result.append(my_dict)
    keys_list.append(elem_key)
    return [result, keys_list]



    Output data

    [
    {'hdf':
    [{'qwe': 'qwevrtyu', 'asd': 'hdf dfgfg', 'zxc': 'fjfjwvb'},
    {'qwe': 'qxcvtyu', 'asd': 'hdf mkvfg', 'zxc': 'kdkd[vb'},
    {'qwe': 'bnmrtyu', 'asd': 'hdf hskfyweee', 'zxc': 'jfjfjfjfb'}]},

    {'ahfh':
    [{'qwe': 'zxcvtyu', 'asd': 'ahfh fhfhf', 'zxc': 'lflflflf'},
    {'qwe': 'qwerbnm', 'asd': 'ahfh sgfdhfovppd', 'zxc': 'lflflflffff'},
    {'qwe': 'qwnm', 'asd': 'ahfh hfovppd', 'zxc': 'lfdddfff'}]},

    {'sgfd':
    [{'qwe': 'qwerbnm', 'asd': 'sgfd dessppd', 'zxc': 'lflflflffff'},
    {'qwe': 'qwerbnm', 'asd': 'sgfd qweerpd', 'zxc': 'lflflflffff'},
    {'qwe': 'qwerbnm', 'asd': 'sgfd kfkfkfd', 'zxc': 'lflflflffff'}]}
    ]


    Now we need to translate this code into Typescript. Almost done, but I'm stuck on small errors.

    export interface Slot {
    qwe: string;
    asd: string;
    zxc: string;
    }


    const processSlots = (data: Array<Slot>) => {
    interface my_dict_interface {
    [key: string]: Array<Slot>
    };
    const result: Array<my_dict_interface> = [];
    const keys_list: string[] = [];
    for (let i = 0; i < data.length; i++) {
    const slot: Slot = data;
    const elem_key: string = slot['asd'].split(" ")[0];
    let my_dict: my_dict_interface;
    if (elem_key in keys_list) {
    for (const my_dict in result) {
    try {
    my_dict[elem_key].add(slot); //Element implicitly has an 'any' type because index expression is not of type 'number'.
    }
    catch (e: unknown) {
    console.log("...ups", e)
    }
    }
    }
    else {
    const elem_value: Slot[] = []
    my_dict[elem_key] = elem_value; // Variable 'my_dict' is used before being assigned.
    elem_value.push(slot);
    result.push(my_dict); // Variable 'my_dict' is used before being assigned.
    keys_list.push(elem_key);
    }
    }
    return [result, keys_list];
    }



    Need output data as in python.

    Continue reading...

Compartilhe esta Página