有没有办法在不使用operator.itemgetter的情况下对嵌套列表进行排序?

发布于 2024-12-14 00:03:33 字数 1128 浏览 2 评论 0原文

我正在阅读一个文件,然后创建我想要对 4 个元素(邮政编码)进行排序的嵌套列表,

jk43:23 Marfield Lane:Plainview:NY:10023
axe99:315 W. 115th Street, Apt. 11B:New York:NY:10027
jab44:23 Rivington Street, Apt. 3R:New York:NY:10002
ap172:19 Boxer Rd.:New York:NY:10005
jb23:115 Karas Dr.:Jersey City:NJ:07127
jb29:119 Xylon Dr.:Jersey City:NJ:07127
ak9:234 Main Street:Philadelphia:PA:08990

这是我的代码:

ex3_3 = open('ex1.txt')
exw = open('ex2_sorted.txt', 'w')

data = []
for line in ex3_3:
    items = line.rstrip().split(':')
    data.append(items)
print sorted(data, key=operator.itemgetter(4))

输出:

[['jb23', '115 Karas Dr.', 'Jersey City', 'NJ', '07127'], ['jb29', '119 Xylon Dr.', 'Jersey City', 'NJ', '07127'], ['ak9', '234 Main Street', 'Philadelphia', 'PA', '08990'], ['jab44', '23 Rivington Street, Apt. 3R', 'New York', 'NY', '10002'], ['ap172', '19 Boxer Rd.', 'New York', 'NY', '10005'], ['jk43', '23 Marfield Lane', 'Plainview', 'NY', '10023'], ['axe99', '315 W. 115th Street, Apt. 11B', 'New York', 'NY', '10027']]

这一切都工作正常,我只是想知道是否有办法做到这一点这不使用“导入运算符”?

I have a file that i'm reading in, then creating nested lists that i want to then sort on the 4 element(zipcode)

jk43:23 Marfield Lane:Plainview:NY:10023
axe99:315 W. 115th Street, Apt. 11B:New York:NY:10027
jab44:23 Rivington Street, Apt. 3R:New York:NY:10002
ap172:19 Boxer Rd.:New York:NY:10005
jb23:115 Karas Dr.:Jersey City:NJ:07127
jb29:119 Xylon Dr.:Jersey City:NJ:07127
ak9:234 Main Street:Philadelphia:PA:08990

Here is my code:

ex3_3 = open('ex1.txt')
exw = open('ex2_sorted.txt', 'w')

data = []
for line in ex3_3:
    items = line.rstrip().split(':')
    data.append(items)
print sorted(data, key=operator.itemgetter(4))

Output:

[['jb23', '115 Karas Dr.', 'Jersey City', 'NJ', '07127'], ['jb29', '119 Xylon Dr.', 'Jersey City', 'NJ', '07127'], ['ak9', '234 Main Street', 'Philadelphia', 'PA', '08990'], ['jab44', '23 Rivington Street, Apt. 3R', 'New York', 'NY', '10002'], ['ap172', '19 Boxer Rd.', 'New York', 'NY', '10005'], ['jk43', '23 Marfield Lane', 'Plainview', 'NY', '10023'], ['axe99', '315 W. 115th Street, Apt. 11B', 'New York', 'NY', '10027']]

this all works fine, I just wonder if there is a way to do this without using "import operator"?

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

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

发布评论

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

评论(3

枫以 2024-12-21 00:03:34

哦,是的,有一个方法:

print sorted(data,key=lambda x: x[4])

Oh yes, there is a way:

print sorted(data,key=lambda x: x[4])
幸福%小乖 2024-12-21 00:03:34

一个粗略的类似方法是:

print sorted(data, key=lambda items: items[4])

但是 operator.itemgetter 更快一点。我使用这个程序对两种方法进行基准测试:

#!/usr/bin/env python

import timeit

withlambda = 'lst.sort(key=lambda items: items[4])'
withgetter = 'lst.sort(key=operator.itemgetter(4))'

setup = """\
import random
import operator
random.seed(0)
lst = [(random.randrange(100000), random.randrange(100000), random.randrange(100000), random.randrange(100000) ,random.randrange(100000))
       for _ in range(10000)]
"""

n = 10000

print "With lambda:"
print timeit.timeit(withlambda, setup, number=n)

print "With getter:"
print timeit.timeit(withgetter, setup, number=n)

它创建一个包含 100,000 个 5 项元组的随机列表,然后在该列表上运行 sort() 1,000 次。在我使用 Python 2.7.2 的 MacBook Pro 上,withlambda 版本的运行时间约为 55.4 秒,withgetter 版本的运行时间约为 46.1 秒。

请注意,随着列表变大,排序算法本身所花费的时间比获取键所花费的时间增长得更快。因此,如果您对大量小列表进行排序,则差异会更大。对包含 1,000 个项目的列表重复运行 100,000 次,结果是 withlambda 需要 22.4 秒,而 withgetter 需要 12.5 秒。

A rough workalike would be:

print sorted(data, key=lambda items: items[4])

but operator.itemgetter is a bit faster. I'm using this program to benchmark both approaches:

#!/usr/bin/env python

import timeit

withlambda = 'lst.sort(key=lambda items: items[4])'
withgetter = 'lst.sort(key=operator.itemgetter(4))'

setup = """\
import random
import operator
random.seed(0)
lst = [(random.randrange(100000), random.randrange(100000), random.randrange(100000), random.randrange(100000) ,random.randrange(100000))
       for _ in range(10000)]
"""

n = 10000

print "With lambda:"
print timeit.timeit(withlambda, setup, number=n)

print "With getter:"
print timeit.timeit(withgetter, setup, number=n)

It creates a random list of 100,000 5-item tuples and then runs sort() on the list 1,000 times. On my MacBook Pro with Python 2.7.2, the withlambda version runs in about 55.4s and withgetter runs in about 46.1s.

Note that as the lists grow large, the time spent in the sorting algorithm itself grows faster than the time spent fetching keys. Therefore, the difference is much greater if you're sorting lots of little lists. Running the same test with a 1,000 item list repeated 100,000 times yields 22.4s for withlambda vs. 12.5s for withgetter.

旧竹 2024-12-21 00:03:34

构建或重新组织您的子列表,以便您要排序的内容排在第一位。在你的例子中,邮政编码应该是元素 0,而不是元素 4。然后你可以对它们进行排序。

当然,还必须考虑这种排序对于数据的其他用途的适用性。

Construct or reorganize your sublist so that the thing you want to sort on is first. In your case, ZIP code, instead of being element 4, should be element 0. Then you can just sort them.

Of course the suitability of this ordering for other uses of the data must also be considered.

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