Python Forum
IP string manipulation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IP string manipulation problem
#5
(Jan-30-2019, 07:01 AM)TheRealNoob Wrote: Thank you a lot!
Really apreciated!

You are welcome Smile

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.
Reply


Messages In This Thread
IP string manipulation problem - by TheRealNoob - Jan-29-2019, 10:03 AM
RE: IP string manipulation problem - by buran - Jan-29-2019, 10:23 AM
RE: IP string manipulation problem - by perfringo - Jan-29-2019, 01:40 PM
RE: IP string manipulation problem - by TheRealNoob - Jan-30-2019, 07:01 AM
RE: IP string manipulation problem - by perfringo - Jan-30-2019, 07:40 AM
RE: IP string manipulation problem - by TheRealNoob - Jan-30-2019, 11:55 AM
RE: IP string manipulation problem - by perfringo - Jan-30-2019, 01:48 PM
RE: IP string manipulation problem - by TheRealNoob - Jan-31-2019, 07:03 AM
RE: IP string manipulation problem - by perfringo - Jan-31-2019, 10:42 AM
RE: IP string manipulation problem - by TheRealNoob - Feb-01-2019, 07:22 AM
RE: IP string manipulation problem - by perfringo - Feb-01-2019, 10:56 AM
RE: IP string manipulation problem - by TheRealNoob - Feb-01-2019, 01:54 PM
RE: IP string manipulation problem - by perfringo - Feb-04-2019, 09:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert string to float problem vasik006 8 3,498 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Problem in list manipulation CyKlop 6 2,372 Oct-18-2021, 09:03 AM
Last Post: DeaD_EyE
  f string concatenation problem growSeb 3 2,306 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,753 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 2,020 Jan-05-2021, 01:33 PM
Last Post: perfringo
  optimization problem for dataframe manipulation fimmu 0 1,494 Aug-31-2020, 06:02 PM
Last Post: fimmu
  string problem Mathisdlg 6 2,931 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 3,098 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 3,727 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Problem with inserting a string in to Sqlite db darktitan 3 4,565 Mar-03-2019, 06:30 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020