Jan-30-2019, 11:55 AM
(This post was last modified: Jan-30-2019, 11:56 AM by TheRealNoob.)
(Jan-30-2019, 07:40 AM)perfringo Wrote: 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):
To respond to your first question related to the "cleanness" of the data, I can say that yes. It will only be IPs, I can be sure of that. Only, I have not considered the case of more than a /24 network into a range.
I cannot exclude my input containing something like 10.1.1.1-10.1.2.31 or worse 10.1.1.1-11.255.1.1, etc. (but it will never have 11.255.1.1-10.1.1.1, that is also sure, the first IP will always be the beginning of the range)
I have not entirely understood how the following parts of the first version work, I don't yet know all the keywords
first, last = [int(el.split('.')[-1]) for el in addr.split('-')] triplet = addr[:addr.split('-')[0].rindex('.')]
and
new.append(f'{triplet}.{i}')In any case I have modified a little your first version to allow me to use a comma separated list list as input and to get the results in the output:
import ipaddress input_string = input("Enter a list element separated by comma ") addresses = input_string. split(',') print (addresses) #to double check what was ingested new = list() for addr in addresses: try: ipaddress.IPv4Address(addr) new.append(addr) except ipaddress.AddressValueError: first, last = [int(el.split('.')[-1]) for el in addr.split('-')] triplet = addr[:addr.split('-')[0].rindex('.')] for i in range(first, last + 1): new.append(f'{triplet}.{i}') print (new)I will have to read better the last codes sample you added, also that is not very clear to me. I am learning
By any means, it is very cool to learn this language and thank you again!!!