Some explanations:
This is roughly equivalent to this:
# 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 :-)
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
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 = lstExplanations 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.
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.