擅长:python、mysql、java
<p><code>sort</code>可以将函数用作执行比较的参数。函数接受两个输入,如果第一个参数更大,则返回<code>1</code>;如果第二个参数更大,则返回{<cd3>};如果为了排序,它们相等,则返回{<cd4>}。这里有一个针对您案例的函数:</p>
<pre><code>def compare_ips(ip1, ip2):
last_octet1 = int(ip1.split('.')[-1]) # splits the ip and grabs the last octet
last_octet2 = int(ip2.split('.')[-1]) # splits the ip and grabs the last octet
if last_octet1 > last_octet2:
return 1
if last_octet1 < last_octet2:
return -1
return 0
</code></pre>
<p>然后只需将其作为关键字参数来排序:</p>
^{pr2}$