chowpay chowpay - 1 month ago 11
Python Question

python loop to find the largest integer from a list

I wrote a script to pull down a list of aws tags and then read the last octect and tell me which one is the highest IP. For example. here is the list of tags that are returned:

['vlslabmc, 172.16.0.13/24', 'vlslabmc,172.16.0.5/24', 'vlslabmc,172.16.0.3/24', 'vlslabmc,172.16.0.12/24', 'vlslabmc,172.16.0.16/24', 'vlslabmc,172.16.0.6/24', 'vlslabmc,172.16.0.1/24', 'vlslabmc,172.16.0.11/24', 'vlslabmc,172.16.0.15/24', 'vlslabmc,172.16.0.17/24', 'vlslabmc,172.16.0.4/24', 'vlslabmc,172.16.0.7/24', 'vlslabmc,172.16.0.10/24', 'vlslabmc,172.16.0.9/24', 'vlslabmc,172.16.0.8/24', 'vlslabmc,172.16.0.2/24', 'vlslabmc,172.16.0.14/24']

Here's my code to workout the largest IP from the tagLis (note that the largest is 17, 172.16.0.17)

21 def findLargestIP():
22 for i in tagList:
23 #remove all the spacing in the tags
24 ec2Tags = i.strip()
25 #seperate any multiple tags
26 ec2SingleTag = ec2Tags.split(',')
27 #find the last octect of the ip address
28 fullIPTag = ec2SingleTag[1].split('.')
29 #remove the CIDR from ip to get the last octect
30 lastIPsTag = fullIPTag[3].split('/')
31 lastOctect = lastIPsTag[0]
32 ipList.append(lastOctect)
33 largestIP = int(ipList[0])
34 for latestIP in ipList:
35 if int(latestIP) > largestIP:
36 largestIP = latestIP
37 return largestIP


I'm not sure why.. but when I print the value of largestIP it always prints out 16. Ive gone through the code it should have worked (I'm avoiding using the max function as I'm just learning to code)

Any help as aways is greatly appreciated.

Thanks

Edit with the answer below, and a question



Ok so thanks to a clue from cmarie I got it working the problem was mainly

33 largestIP = int(ipList[0])


Here's the code running before with an added print statement on the list append:

'13']
['13', '5']
['13', '5', '3']
['13', '5', '3', '12']
['13', '5', '3', '12', '16']
16
['13', '5', '3', '12', '16', '6']
16
['13', '5', '3', '12', '16', '6', '1']
16
['13', '5', '3', '12', '16', '6', '1', '11']
16
... ...
['13', '5', '3', '12', '16', '6', '1', '11', '15', '17', '4', '7', '10', '9', '8', '2']
16
['13', '5', '3', '12', '16', '6', '1', '11', '15', '17', '4', '7', '10', '9', '8', '2', '14']
16


Basically what was happening is that during this loop :

33 largestIP = int(ipList[0])
34 for latestIP in ipList:
35 if int(latestIP) > largestIP:
36 largestIP = latestIP


The loop stops at the 1st largest integer. in this case that is 16. *I'm not sure why it does but it does

Here's the working code:

19 def findLargestIP():
20 ipList =[]
21 for i in tagList:
22 #remove all the spacing in the tags
23 ec2Tags = i.strip()
24 #seperate any multiple tags
25 ec2SingleTag = ec2Tags.split(',')
26 #find the last octect of the ip address
27 fullIPTag = ec2SingleTag[1].split('.')
28 #remove the CIDR from ip to get the last octect
29 lastIPsTag = fullIPTag[3].split('/')
30 lastOctect = lastIPsTag[0]
31 ipList.append(int(lastOctect))
32 print ipList
33 largestIP = 0
34 for latestIP in ipList:
35 if latestIP > largestIP:
36 largestIP = latestIP
37 print latestIP
38 print largestIP
39 return largestIP


and the result:

[13, 5, 3, 12, 16]
13
16
[13, 5, 3, 12, 16, 6]
13
16
[13, 5, 3, 12, 16, 6, 1]
13
16
[13, 5, 3, 12, 16, 6, 1, 11]
13
16
[13, 5, 3, 12, 16, 6, 1, 11, 15]
13
16
[13, 5, 3, 12, 16, 6, 1, 11, 15, 17]
13
16
17


Note it found 17.

Answer

Although other people have already provide you some alternative ways to find the answer, if you want to keep using your program, here is some way of fixing it:

def findLargestIP():
    ipList = []
    for i in tagList:
        #remove all the spacing in the tags
        ec2Tags = i.strip()
        #seperate any multiple tags
        ec2SingleTag = ec2Tags.split(',')
        #find the last octect of the ip address
        fullIPTag = ec2SingleTag[1].split('.')
        #remove the CIDR from ip to get the last octect
        lastIPsTag = fullIPTag[3].split('/')
        lastOctect = lastIPsTag[0]
        ipList.append(int(lastOctect))
    largestIP  = 0
    for latestIP in ipList:
        if int(latestIP) > largestIP:
            largestIP = latestIP
    return largestIP

The differences from this and your program are that here I:

  • set ipList = []
  • make ipList contain integers, rather than strings
  • set largestIP = 0, instead of taking the first number of the ipList (since you shouldn't assume the list is sorted)
  • remove the loop to find the largest number outside the first loop [on the tagList] - just for eliminating unnecessary iterations

If I would do that task, however, I would try to use regular expressions. Here is a way to do it:

import re
def alternativeFindLargestIP():
    ipList = re.findall(r'(?<=\.)\d+(?=/)', ' '.join(tagList))
    ipList = [int(num) for num in ipList]
    return max(ipList)
Comments