(Jan-30-2019, 07:01 AM)TheRealNoob Wrote: Thank you a lot!
Really apreciated!
You are welcome
If your source data is 'clean' and therefore there is no need to be 'defensive' it is possible to get same result without using ipaddress module (in first iteration it was used as convenient way to determine whether string is IPv4 or not). If all strings are guaranteed to be valid addresses or ranges containing '-' code can be simplified: moving getting addresses in range to separate function (how it's done) and using simple if clause (what is done):
def ip_from_range(ip_range): """Return list of ip addresses in ip_range. Separate ip addresses on '-', get value after last '.', convert to int and assign to names first, last. Separate ip addresses on '-', get first address, get index of '.' from right, construct slice from address, get string consisting ip address first three groups (without comma) and assign to name triplet. With list comprehension create list of ip addresses in range from first to last by joining triplet and range values. :param ip_range: first and last ip address separated by '-' :type ip_range: str :return: list of ip addresses in range :rtype: list """ first, last = [int(el.split('.')[-1]) for el in ip_range.split('-')] triplet = addr[:ip_range.split('-')[0].rindex('.')] return [f'{triplet}.{i}' for i in range(first, last + 1)]Now the code can be written:
new = [] for addr in addresses: if '-' in addr: new.extend(ip_from_range(addr)) else: new.append(addr)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.