Hi I am new. Here is basic question. Plese see the below attahced.
What is in second line and 4th line? Thank you very much
IPs = ['192.168.1.1', '192.168.2.1-10']
expanded_ip_list = **[]**
for ip in IPs:
if '-' in ip.split('.')[3]:
prefix = '.'.join(ip.split('.')[:3])
high_low = ip.split('.')[3].split('-')
r = range(int(high_low[0]), int(high_low[1])+1, 1)
for x in r:
expanded_ip_list.append('{0}.{1}'.format(prefix,x))
else:
expanded_ip_list.append(ip)
print(expanded_ip_list)
The [] in the second line is an empty list. It’s much like the list in the first line, except that it contains nothing!
The [3] on the fourth line accesses the fourth element (because we count from 0) of ip.split('.'). So that’ll be the last octet of the IP addresses in the first line. Try adding print(ip.split('.')) on a new line just after the for:
IPs = ['192.168.1.1', '192.168.2.1-10']
expanded_ip_list = **[]**
for ip in IPs:
print(ip.split('.'))
Run it and see what you get. Then try adding the [3] bit in and see how the result changes.
The square bracket syntax [ ] has two meanings in Python.
The first is to create a list. The official name for this is list display.
a = [1, 2, 3, 4] # List with four items.
b = ['hello', 'world'] # List with two items.
c = [] # List with no items, an empty list.
We can also create a list using list comprehension syntax:
items = [2*x + 1 for x in range(10)] # List comprehension
List comprehensions are a short, compact version of a for-loop.
The second meaning is called subscripting, or sometimes indexing. Subscripts always have to immediately follow some expression that returns a value. The simplest expression is a variable name, so I will use that.
print( items[5] ) # Print the item inside items at index 5
print( items[2] ) # Print the item inside items at index 2
print( items[1:4] ) # Print the sublist of items from index 1 through 4 (a "slice").
mydict = {'colour': 'green', 'size': 'small', 'cost': 19.99}
print( mydict['colour'] ) # Print the value with key 'colour'
So we can use a subscript to get items from a list or dict. For lists (and strings), you have to give the index position. Remember that in Python, the first item is index 0, the second item is index 1, and so on. For dicts, the subscript is a key.
The fourth line if '-' in ip.split('.')[3]:
is checking the fourth set of characters to see if they contain a dash, so that on the first run ip = 192.168.1.1 and then the split operator gives the set of digits in turn 192 then 168 etc.
the [3] operator ‘looks’ at the the fourth element which is the last set of digits (in this case 1).
The ‘if’ is looking to find a dash ‘-’ and for this first run through there is none hence the ‘if’ fails and returns execution to the outer ‘for’ loop.
Next run of the for loop makes ip = 192.168.2.1-10 and the within the ‘if’ the [3] picks up the last digits 10. Then body of the ‘if’ block runs prefix = ...
By davidy2001 via Discussions on Python.org at 20Aug2022 14:53:
Thank you very for your reply!
" if ‘-’ in ip.split(‘.’)[3]: " ------ i think “ip” in this command should be “IPs”, why it is “ip”?
Because it is inside a loop:
for ip in IPs:
print(ip.split('.'))
This for-loop iterates through the strings in the list IPs. Inside the
loop, the variable ip refers to the value of just one of those
strings, in turn.