Home > Software design >  How to iterate through two nested lists (28 and 3 elements) and check if subset based on the length
How to iterate through two nested lists (28 and 3 elements) and check if subset based on the length

Time:11-25

I would like to check if values from the list1 are part of a subset of list2 ([cor[1] for cor in list2]) based on the length of list1.

The result should be a list of 28 elements len(list1) and look like this:

[['1'], [43, 44, 45, 46, 47, 48], [43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71], "YES")],
[([0, 1, 2, 3, 4, 5, 6, 7], "NO")]
list2 = [[['1'],
           [43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
            62, 63, 64, 65, 66, 67, 68, 69, 70, 71]],
          [['1'],
           [117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
            132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145]],
          [['2'],
           [272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286,
            287, 288]]]

list1 = [[0, 1, 2, 3, 4, 5, 6, 7],
        [8, 9, 10, 11, 12, 13],
        [18, 19, 20, 21, 22, 23],
        [24, 25, 26, 27, 28, 29, 30, 31],
        [32, 33, 34, 35, 36],
        [43, 44, 45, 46, 47, 48],
        [53, 54, 55, 56, 57, 58],
        [59, 60, 61, 62, 63, 64, 65, 66],
        [67, 68, 69, 70, 71],
        [76, 77, 78, 79, 80, 81, 82, 83],
        [88, 89, 90, 91, 92],
        [101, 102, 103, 104, 105, 106, 107, 108, 109],
        [111, 112, 113, 114, 115, 116],
        [117, 118, 119, 120, 121, 122],
        [127, 128, 129, 130, 131, 132],
        [133, 134, 135, 136, 137, 138, 139, 140],
        [141, 142, 143, 144, 145],
        [158, 159, 160, 161, 162, 163],
        [164, 165, 166, 167, 168, 169],
        [188, 189, 190, 191, 192, 193, 194, 195],
        [196, 197, 198, 199, 200, 201, 202, 203, 204, 205],
        [221, 222, 223, 224, 225],
        [236, 237, 238, 239, 240, 241, 242],
        [243, 244, 245, 246, 247, 248],
        [276, 277, 278, 279, 280, 281, 282],
        [283, 284, 285, 286, 287, 288],
        [313, 314, 315, 316, 317],
        [318, 319, 320, 321, 322, 323]]
for ent in list1:
    for cor in list2:
        if set(ent).issubset(cor[1]) == True:
            print(cor[0], ent, cor[1], "YES")
        else:
            print("NO")

The code above gives me the result 84 times, which is the iteration of 28 and 3 elements of both lists, therefore rises a question: Is it even possible?

CodePudding user response:

If this is a small example it is fine to write with two nested loop, however for correct complexity you could put your list2 in a dictionary (since all integers are sequential pretty easily to hash them as a range) and only do with one loop.

But back to your implementation:

you do two time loop and in each scenario you print something, that is expected to have 28 * 3 time. What you need is that in your inner loop you just print after it is finished.

for ent in list1:
    idx = [set(ent).issubset(cor[1]) for cor in list2]
    if any(idx):
        i = idx.index(True)
        print(list2[i][0], ent, list2[i][1], "YES")
    else:
        print("NO")
  • Related