[Python] Organizing shared items on an object

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

  1. Stack

    Stack Membro Participativo

    In the attached image, you can see that some wires are sharing the same dot and their wire spans are connecting, i.e., Wire#1-Wire#4, Wire#2-Wire#5, etc. In other words, Wire#4, Wire#5, Wire#6 are on one side of the pole and the Wire#1, Wire#2, Wire#3 on the other side. Wires on both sides connect to each-other through a common dot.

    I would like to have these shared wires in one array and the ones the don't share a dot should be in a separate array. The code looks right to me but I am not too sure why I am getting the results as shown.

    [​IMG]

    The results I am looking for:

    [["Wire#7", "Wire#8"], ["Wire#9"],["Wire#1_Wire#4", "Wire#2_Wire#5", "Wire#3_Wire#6"], ["Wire#10_Wire#11"]]


    The results I am getting:

    [['Wire#7', 'Wire#8'], ['Wire#9'], ['Wire#1_Wire#4'], ['Wire#5_Wire#2'], ['Wire#3_Wire#6'], ['Wire#11_Wire#10']]


    Python Code:

    list_1 = ["Wire#1","Wire#2", "Wire#3"]
    list_2 = ["Wire#4","Wire#5", "Wire#6"]
    list_3 = ['Wire#7', "Wire#8"]
    list_4 = ["Wire#9"]
    list_5 = ["Wire#11"]
    list_6 = ["Wire#10"]


    all_list = [list_1, list_2, list_3,list_4, list_5, list_6]


    pairs = [
    ["Wire#1", "Wire#4"],
    ["Wire#5", "Wire#2"],
    ["Wire#3","Wire#6"],
    ["Wire#7"],
    ["Wire#8"],
    ["Wire#9"],
    ["Wire#11", "Wire#10"],
    ]

    final_wire_list = []
    new_wire_list = []


    for i in range(0, len(pairs)):

    if len(pairs) > 1:
    new_text = ""
    for c in range(0, len(pairs)):
    if (c+1) != len(pairs):
    new_text += pairs[c] + "_"
    else:
    new_text += pairs[c]
    new_wire_list.append([new_text])

    for j in range(0, len(pairs)):
    new = []
    for ii in range(0, len(all_list)):
    o = []
    for jj in range(0, len(all_list[ii])):
    if pairs[j].lower() != all_list[ii][jj].lower():
    o.append(all_list[ii][jj])
    new.append(o)


    all_list = new


    for i in range(0, len(all_list)):
    if len(all_list) != 0:
    final_wire_list.append(all_list)


    for i in range(0, len(new_wire_list)):
    if len(new_wire_list) != 0:
    final_wire_list.append(new_wire_list)


    print(final_wire_list)

    Continue reading...

Compartilhe esta Página