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

Issues comparing two lists

I am starting with a string of MAC addresses and a list of MAC addresses:

s3_mac_string = "[['50:9a:4c:17:b4:f5', '54:13:79:70:6b:97', '54:13:79:70:6b:98', '56:13:79:70:6b:97', '66:13:79:70:6b:97'], ['c8:34:8e:72:3f:89', 'c8:34:8e:72:3f:8a', 'ca:34:8e:72:3f:89'], ['54:ee:75:b7:9f:a4', 'f0:d5:bf:bf:65:ee', 'f0:d5:bf:bf:65:ef', 'f2:d5:bf:bf:65:ee']]"
huntress_unseen_mac = [['80:86:f2:da:dd:ce', '80:86:f2:da:dd:cf', '80:86:f2:da:dd:d2', '82:86:f2:da:dd:ce', 'ec:f4:bb:40:25:be'], ['10:60:4b:73:39:db'], ['a4:bb:6d:3f:ed:10'], ['a4:bb:6d:3e:7b:e1'], ['d0:3c:1f:77:84:b3', 'd0:3c:1f:77:84:b4', 'f0:d5:bf:bf:65:ee', 'd0:3c:1f:77:84:b7']]

I am then converting string1 to a list with this function:

def Convert(string):
    string = string.replace("[", "").replace("]", "").replace('"', "").replace("'", "")
    li = list(string.split(","))
    return li

My main():

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

def main():
    s3_mac_string = get_mac_addresses_from_s3()
    huntress_unseen_mac = get_unseen_mac_huntress()
    s3_mac_list = Convert(s3_mac_string)

    huntress_unseen_mac_lower = [
        str(huntress_unseen_mac).lower().strip()
        for huntress_unseen_mac in huntress_unseen_mac
    ]

    s3_mac_list_lower = [str(s3_mac).lower().strip() for s3_mac in s3_mac_list]

    common_macs = []
    for mac in huntress_unseen_mac_lower:
        if mac in s3_mac_list_lower:
            common_macs.append(mac)

    print(f"Common MACs: {common_macs}")

The printing of the common macs is just printing an empty list eventhough there are commonalities between the lists. As a troubleshooting step I appended the huntress_unseen_mac_lower with huntress_unseen_mac_lower.appent("<MAC THAT IS IN S3_MAC_LIST")

I also tried using set intersection initially and that didn’t work either.

    huntress_unseen_mac_lower = [
        str(huntress_unseen_mac).lower() for huntress_unseen_mac in huntress_unseen_mac
    ]
    s3_mac_list_lower = [str(s3_mac).lower() for s3_mac in s3_mac_list]

    common_macs = set(huntress_unseen_mac_lower).intersection(s3_mac_list_lower)

    print(f"Common Macs: {list(common_macs)}")

Is there something I am missing?

>Solution :

You can build a list of lists from s3_mac_string using ast.literal_eval.

From that, build a set from all the sub-elements.

Build another set from huntress_unseen_mac (which is already a list of lists).

Print the intersection of the two sets.

from ast import literal_eval

def as_set(_list: list[list[str]]) -> set:
    s = set()
    s.update(*_list)
    return s

s3_mac_string = "[['50:9a:4c:17:b4:f5', '54:13:79:70:6b:97', '54:13:79:70:6b:98', '56:13:79:70:6b:97', '66:13:79:70:6b:97'], ['c8:34:8e:72:3f:89', 'c8:34:8e:72:3f:8a', 'ca:34:8e:72:3f:89'], ['54:ee:75:b7:9f:a4', 'f0:d5:bf:bf:65:ee', 'f0:d5:bf:bf:65:ef', 'f2:d5:bf:bf:65:ee']]"
huntress_unseen_mac = [['80:86:f2:da:dd:ce', '80:86:f2:da:dd:cf', '80:86:f2:da:dd:d2', '82:86:f2:da:dd:ce', 'ec:f4:bb:40:25:be'], ['10:60:4b:73:39:db'], ['a4:bb:6d:3f:ed:10'], ['a4:bb:6d:3e:7b:e1'], ['d0:3c:1f:77:84:b3', 'd0:3c:1f:77:84:b4', 'f0:d5:bf:bf:65:ee', 'd0:3c:1f:77:84:b7']]

s3_mac_set = as_set(literal_eval(s3_mac_string))
h_mac_set = as_set(huntress_unseen_mac)

print(s3_mac_set & h_mac_set)

Output:

{'f0:d5:bf:bf:65:ee'}
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