I have a list that is dynamically generated:
myList = [[node1, mask1],
[node2, mask1],
[node3, mask1],
[node4, mask2],
[node5, mask2],
[node6, mask3],
[node7, mask4],
]
Note: each node/mask in the list are actual Nodes in the software GUI, that I am trying to access and manipulate later. I think representing them as strings for now, would serve the purpose fine.
Rules:
- I must logically compare each item in the list with each other to get the result;
keep all the nodes except those that have connection to only one type of mask, in this example, node6, and 7, needs to be excluded and get the following result:
newList = [[node1, node2, node3], [node4, node5]]
Optional: I would also like to keep some information of each node to what mask has been connected so I can use that later - but I can think of other solutions to this so it's optional.
I tried looping through each element with nested for loops but that misses some cases. I also tried working with groupby()
, but I can't figure it out, as my Python knowledge is limited.
You can use a
defaultdict
to group all nodes with same mask into a list:The
node_gps
dict also stores the mask associated with each list of nodes.