Skip to content Skip to sidebar Skip to footer

How To Efficiently Count The Occurrences Of Mac Values By Distinct Interface Value When These Are Inside List Of Dictionaries?

In other words given the list of dictionaries how could I efficiently end up with a list of interfaces which have multiple mac addresses tied to them? In the example below we would

Solution 1:

You could use a defaultdict to keep track of how many times a particular interface name is seen.

Then you would need to filter out the interfaces that are seen only once.

interfaces = defaultdict(int)
for iface in data['mac_address_table']:
    if not iface['interface']:
        continue
    interfaces[iface['interface']] += 1

multi_mac_interfaces = [iface for iface, count in interfaces.items() if count > 1]
print(multi_mac_interfaces)

Solution 2:

If your input is large and you can't use json parser (RAM limitation), you should process it line by line. Fastest way to check for duplicates will be using set also using set as container for result you don't need to think about duplicates (duplicate entries won't be added):

text = "\"interface\": \""
text_len = len(text)
delimiter = "\""

withopen("FILENAME") as in_f:
    unique_keys =set()
    result=set()
    value= ""
    for line in in_f:
        if valueand "}" in line: # detecting endof object
            if valuein unique_keys:
                result.add(value)
            else:
                unique_keys.add(value)
            value= ""
        else:
            idx = line.find(text) + text_len
            if idx >= text_len:
                value= line[idx: line.find(delimiter, idx)]

for i inresult:
    print(i)

Post a Comment for "How To Efficiently Count The Occurrences Of Mac Values By Distinct Interface Value When These Are Inside List Of Dictionaries?"