有没有办法在不使用operator.itemgetter的情况下对嵌套列表进行排序?
我正在阅读一个文件,然后创建我想要对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哦,是的,有一个方法:
Oh yes, there is a way:
一个粗略的类似方法是:
但是
operator.itemgetter
更快一点。我使用这个程序对两种方法进行基准测试:它创建一个包含 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:
but
operator.itemgetter
is a bit faster. I'm using this program to benchmark both approaches: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, thewithlambda
version runs in about 55.4s andwithgetter
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 forwithgetter
.构建或重新组织您的子列表,以便您要排序的内容排在第一位。在你的例子中,邮政编码应该是元素 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.