Sep-08-2022, 11:45 AM
This makes the code shorter:
from ipaddress import IPv4Address, ip_address def make_ipv4_set(file_like): results = set() for line in map(str.strip, file_like): try: addr = ip_address(line) except ValueError: continue if isinstance(addr, IPv4Address): results.add(addr) return results with ( open("ip1_file.txt") as ip1_file, open("ip2_file.txt") as ip2_file, ): ip1_set = make_ipv4_set(ip1_file) ip2_set = make_ipv4_set(ip2_file) # sorting works because IPv4Address is sortable results = sorted(ip1_set - ip2_set) # results is now a list. sorted returns always a list # iterate over the elements and print them for ip in results: # converting IPv4Address to str print(str(ip))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!