Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a list of edges in python corresponding to a set?

# inputs

edges = [ [1,2] , [2,3] , [3,4] , [4,5] , [5,2] , [4,6] , [6,7] , [7,6] , [7,8] ]

sets = [ [2,3,4,5] , [6,7] ]

# output

sets_of_edges = [ [ [2,3] , [3,4] , [4,5] , [5,2] ] , [ [6,7] , [7,6] ] ]

How do I write a code that takes ‘edges’ and ‘sets’ and outputs ‘sets_of_edges’, by going through each set in ‘sets’ and getting the sets of edges that contain both of each value in sets (if that makes sense). I have written an example input and outputs to help explain. Thanks! 🙂

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is a quick code sample that should solve your problem:

# inputs

edges = [[1, 2], [2, 3], [3, 4], [4, 5], [
    5, 2], [4, 6], [6, 7], [7, 6], [7, 8]]

sets = [[2, 3, 4, 5], [6, 7]]

# output

sets_of_edges = [[[2, 3], [3, 4], [4, 5], [5, 2]], [[6, 7], [7, 6]]]

res = []
for current_set in sets:
    temp = []
    for current_edge in edges:
        if current_edge[0] in current_set and current_edge[1] in current_set:
            temp.append(current_edge)
    res.append(temp)

print(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading