如何在 Python 中对 IP 地址和整数进行排序?

发布于 2024-10-07 02:13:01 字数 534 浏览 1 评论 0原文

我有一个 IP 和整数列表,我希望 python 按第四列排序:

172.2.174.86 172.2.15.65 69694 42272874

172.2.200.100 172.2.15.20 14 4326

10.1.162.12 172.2.15.1 62 4741 170676

172.2.174.86 172.2 .15.64 46021 33956341

10.1.167.237 172.2.15.69 921 133574

问题是Python似乎无法处理同一列表中的IP地址和整数。我只能按字母顺序排序。如何根据第四列的值进行正确排序这是我所拥有的:

lines = open("file.txt", "r").readlines()

lines=[x.split() for x in lines]

for i in lines:
 i.reverse()

lines.sort(cmp, reverse=True)

for i in lines:
 print i

I have a list of IPs and integers that I'd like to python to sort by the 4th column:

172.2.174.86 172.2.15.65 69694 42272874

172.2.200.100 172.2.15.20 14 4326

10.1.162.12 172.2.15.162 4741 170676

172.2.174.86 172.2.15.64 46021 33956341

10.1.167.237 172.2.15.69 921 133574

The problem is Python can't seem to handle IP addresses and integers in the same list. I can only sort alphabetically. How do I do correct sorting based on the value of the 4th column Here's what I have:

lines = open("file.txt", "r").readlines()

lines=[x.split() for x in lines]

for i in lines:
 i.reverse()

lines.sort(cmp, reverse=True)

for i in lines:
 print i

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烧了回忆取暖 2024-10-14 02:13:02

你的ip整数存储在字符串中,对吧?第四列是字符串的一部分?但是你可以做类似的事情

l.sort(key=lambda x:x.split()[3], reverse=True)

,或者如果你想要更多的控制,你可以定义一个函数,它接受 2 个字符串并确定哪个是最高的,然后传递该函数来对 cmp 参数进行排序

you ip integers are stored in string, right? and the 4th column is part of the string? however you can do something like

l.sort(key=lambda x:x.split()[3], reverse=True)

or if you want more control, you can define a function that takes 2 string and determine wich is the highest, then pass that func to sort through cmp argument

沫尐诺 2024-10-14 02:13:02
lines = open("file.txt", "r").readlines()
lines=[x.split() for x in lines]
lines.sort(key=lambda l: int(l[3]), reverse=True)
lines = open("file.txt", "r").readlines()
lines=[x.split() for x in lines]
lines.sort(key=lambda l: int(l[3]), reverse=True)
如日中天 2024-10-14 02:13:02

我认为你不需要改变你的路线。 您应该能够简单地对行进行排序

lines.sort( lambda x, y: cmp( int(x[3]), int(y[3]) ) )

假设第四列中始终有一个整数值,

。讨论 Python 序列以及如何操作它们的好链接:Effbot

I don't think you need to reverse your lines. You should be able to simply sort the lines using

lines.sort( lambda x, y: cmp( int(x[3]), int(y[3]) ) )

assuming that you always have an integral value in your fourth column.

Good link discussing Python sequences and how to manipulate them: Effbot

苏别ゝ 2024-10-14 02:13:01

以下是您所追求的吗?

lines = open("file.txt", "r").readlines()
lines = [x.split() for x in lines]
lines.sort(cmp, key=lambda x:int(x[3]))
for i in lines:
  print i

Is the following what you're after?

lines = open("file.txt", "r").readlines()
lines = [x.split() for x in lines]
lines.sort(cmp, key=lambda x:int(x[3]))
for i in lines:
  print i
你又不是我 2024-10-14 02:13:01
import csv

with open("file.txt") as f:
    data = list(csv.reader(f, delimiter=' '))

def intkey(row):
    return int(row[3])

data.sort(key=intkey, reverse=True)
print data

结果:

[['172.2.174.86', '172.2.15.65', '69694', '42272874'],
 ['172.2.174.86', '172.2.15.64', '46021', '33956341'],
 ['10.1.162.12', '172.2.15.162', '4741', '170676'],
 ['10.1.167.237', '172.2.15.69', '921', '133574'],
 ['172.2.200.100', '172.2.15.20', '14', '4326']]
import csv

with open("file.txt") as f:
    data = list(csv.reader(f, delimiter=' '))

def intkey(row):
    return int(row[3])

data.sort(key=intkey, reverse=True)
print data

the results:

[['172.2.174.86', '172.2.15.65', '69694', '42272874'],
 ['172.2.174.86', '172.2.15.64', '46021', '33956341'],
 ['10.1.162.12', '172.2.15.162', '4741', '170676'],
 ['10.1.167.237', '172.2.15.69', '921', '133574'],
 ['172.2.200.100', '172.2.15.20', '14', '4326']]
栩栩如生 2024-10-14 02:13:01

我找不到在没有长复杂函数和 lambda 的 py 中完成此操作的干净简单的方法,

到目前为止最简单的方法,

pip install sh

from sh import sort
sort('-nr', '/tmp/file1', '-o', '/tmp/file1')

sh 模块非常棒。

I couldnt find clean easy way to do this in py w/o long complex functions and lambdas,

easiest way by far,

pip install sh

from sh import sort
sort('-nr', '/tmp/file1', '-o', '/tmp/file1')

sh module is fantastic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文