Python Forum
IP string manipulation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IP string manipulation problem
#7
Some explanations:

first, last = [int(el.split('.')[-1]) for el in addr.split('-')]
On right from = there is list comprehension. This returns list which is unpacked and values assigned to names first and last.

This is roughly equivalent to this:

lst = list()                       

for el in addr.split('-'):        
    lst.append(int(el.split('.')[-1]))

first, last = lst
Explanations by row numbers:


# 1 - we create list where we will accumulate results
# 3 - addr.split('-') - using split method on string we convert '10.1.1.4-10.1.1.5' to ['10.1.1.4', '10.1.1.5']. for-loop goes through all elements in list, one at a time (actually it's iterator which is created by Python autamagically with for-loop from result of .split() method but in this context it is not important).
# 4 - each element i.e. '10.1.1.4' and '10.1.1.5' is splitted again using el.split('.') resulting ['10', '1', '1', '4'] and ['10', '1', '1', '5'] respectively. From lists last element is retrieved using indexing ([-1]) and converted to int (int(..)). This int is appended (lst.append(..)) to list we created for accumulating results. When loop has finished lst value will be [4, 5]
# 6 - list is unpacked and values assigned to names first and last (4 and 5 respectively)

As you can see, list comprehension makes it more compact :-)

triplet = addr[:addr.split('-')[0].rindex('.')]
Here we use indexing again. On right side from = there is slice made from addr (good reading material from StackOverflow: How slicing works). Basically with addr.split('-')[0] we are performing following steps: '10.1.1.4-10.1.1.5' --> ['10.1.1.4', '10.1.1.5'] --> '10.1.1.4'. Now we have a string and with rindex we return index of first '.' starting from right. It this specific case it will be integer 6 and this code evaluates to addr[:6]. Result of this slice '10.1.1' is assinged to name triplet.

f'{triplet}.{i}'
This is Literal String Interpolation a.k.a f-string. This quite new addition to Python (available in Python 3.6 and later). This code actually constructs new string from two values (triplet and i). Something like that {triplet}.{i} --> {'10.1.1'}.{4} and {'10.1.1'}.{5} respectively --> '10.1.1.4' and '10.1.1.5' respectively.

Hopefully it helps to understand :-)

Does 10.1.1.1-11.255.1.1 mean that there are all ip addresses in that space a la all combinations from:

10.1-255.1-255.1-255 + 11.1-255.1.1
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,519 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Problem in list manipulation CyKlop 6 2,390 Oct-18-2021, 09:03 AM
Last Post: DeaD_EyE
  f string concatenation problem growSeb 3 2,322 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,769 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 2,043 Jan-05-2021, 01:33 PM
Last Post: perfringo
  optimization problem for dataframe manipulation fimmu 0 1,503 Aug-31-2020, 06:02 PM
Last Post: fimmu
  string problem Mathisdlg 6 2,952 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 3,118 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 3,748 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Problem with inserting a string in to Sqlite db darktitan 3 4,588 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