[Python] Efficient way to compare nested lists based on value(s), then once found change...

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have 2 nested lists of widgets - each individual list in both nested lists has the same number of items & format (all items in a list are strings) - e.g.

    widgets = [['a', 'b', 'c', ..., 'z'], ['a', 'b', 'c', ..., 'z'],..., ['a', 'b', 'c', ..., 'z']]
    op_widgets = [['a', 'b', 'c', ..., 'z'], ['a', 'b', 'c', ..., 'z'],..., ['a', 'b', 'c', ..., 'z']]


    Lists widgets & op_widgets can have up to 30,000 lists, & 'e' is the unique key for each list.

    What I need to do is -

    • Match each list in widgets with a list in op_widgets (based on matching key 'e')
    • If it exists, make changes to the found op_widgets list in the db.
    • If a list in op_widgets matches specific items in a widgets list except the key 'e' & a specific item ('h'), make changes to the found op_widgets list in the db.
    • I then also may need to remove the list from widgets once it's been found in op_widgets & the changes are made to the op_widgets list.

    I'm using nested for loops at the moment to do this -

    for ea in events:
    for i,v in enumerate(op_widgets):
    ex = op_widgets
    if (ea[4] in op_widgets)) or (ea[4] not in op_widgets and ea[7] != ex[7] and (ea[5] == ex[5] and ea[8] == ex[8] and ea[9] == ex[9] and ea[10] == ex[10] and ea[11] == ex[11] and ea[12] == ex[12] and ea[13] == ex[13] and ea[14] == ex[14] and ea[15] == ex[15] and ea[16] == ex[16] and ea[17] == ex[17] and ea[18] == ex[18] and ea[19] == ex[19] and ea[20] == ex[20] and ea[21] == ex[21] and ea[22] == ex[22] and ea[23] == ex[23] and ea[24] == ex[24] and ea[25] == ex[25]))):


    I then use the key (ex[4]) to find & update the widget in the database using an sql query.

    This works but takes a long time.

    Continue reading...

Compartilhe esta Página