Unexpected output while sorting the list of IP address [duplicate]

2024/10/10 16:19:05

I am trying to sort the list of ipaddress from the following list.

IPlist= ['209.85.238.4', '216.239.51.98', '64.233.173.198', '64.3.17.208', '64.233.173.238']

#1st case

tmp1 = [list (map (str, ip.split("."))) for ip in IPlist]tmp1.sort()
print(tmp1)

When I run this snippet. I got the following output.

[['209', '85', '238', '4'], ['216', '239', '51', '98'], ['64', '233', '173', '198'], ['64', '233', '173', '238'], ['64', '3', '17', '208']]

#second case

tmp = [tuple (map (int, ip.split("."))) for ip in IPlist]
# print(tmp)tmp.sort ()
print(tmp)

When I run the second case,I got the following output.

[(64, 3, 17, 208), (64, 233, 173, 198), (64, 233, 173, 238), (209, 85, 238, 4), (216, 239, 51, 98)]

The only thing that I found the difference is the conversion to either string or int function using above function. But even if the intial values are string, doesnt sort() function works in the same way?

For eg:

lst = ['23', '33', '11', '7', '55']# Using sort() function with key as int
lst.sort(key = int)print(lst)
Output: ['7', '11', '23', '33', '55']
Answer

This happens because the addresses are being compared as strings, which are sorted lexically - converting them to numbers will sort the way you expect

>>> sorted(["11", "2"])
['11', '2']
>>> sorted([11, 2])
[2, 11]

However, it's probably better overall to use the builtin ipaddress module, which can sort the addresses correctly for you and has useful methods and comparisons for IP addresses (such as checking whether an IP is a member of a subnet or providing the network address of an interface with an IP and mask)

>>> import ipaddress
>>> ip_list = ['209.85.238.4', '216.239.51.98', '64.233.173.198', '64.3.17.208', '64.233.173.238']
>>> sorted(ipaddress.ip_address(a) for a in ip_list)
[IPv4Address('64.3.17.208'), IPv4Address('64.233.173.198'), IPv4Address('64.233.173.238'), IPv4Address('209.85.238.4'), IPv4Address('216.239.51.98')]
https://en.xdnf.cn/q/118429.html

Related Q&A

Google Cloud Run returning Server Unavailable Occasionally

I am running a Flask app at https://recycler-mvdcj7favq-uc.a.run.app/ on Google Cloud Run and occasionally I get 503 server unavailable while refreshing a few times seems to load the page. Also, someti…

Connecting to Internet?

Im having issues with connecting to the Internet using python.I am on a corporate network that uses a PAC file to set proxies. Now this would be fine if I could find and parse the PAC to get what I nee…

Angular App Not Working When Moving to Python Flask

Not sure what information to give so will do as much as I can. Currently have an Angular app sitting on IIS and using Classic ASP. All works fine. There is a dropdown which fetches some JSON that then …

How can I subtract tuples in a list?

Lets say I have a list with tuples in it.Something like this:listnum = [(18,12),(12,20)]Is there a way I can subtract what is in the tuples and make listnum into:listnum = [6,8]As you can see It takes …

Chart barh matplotlib - overlap bars

Im new user of matplotlib and I have a problem with chart barh: overlap bars. When plot the graph, the bars draws overlapped and I havent found the reason. In my opinion the problem is on re-size the …

Python django image upload is not working

Python Django image upload is not working when I am adding data and click on submit button not give a any response and also not upload any data in the database.models.py fisrst of all i add model file.…

Android bluetooth send message working first time only

I need to send string message from Raspberry PI to Android device. I am getting message first time only. After that it does not work at all. I am using PYTHON code in Raspberry PI. After first time, it…

Scraping data from href

I was trying to get the postcodes for DFS, for that i tried getting the href for each shop and then click on it, the next page has shop location from which i can get the postal code, but i am able to g…

Numpy - how to sort an array of value/key pairs in descending order

I was looking at the problem Fastest way to rank items with multiple values and weightings and came up with the following solution, but with two remaining issues:import numpy as np# set up values keys …

How to extract certain under specific condition in pandas? (Sentimental analysis)

The picture is what my dataframe looks like. I have user_name, movie_name and time column. I want to extract only rows that are first day of certain movie. For example, if movie as first date in the ti…