简体   繁体   English

你如何根据第二个“列”对 python 中 1.72e+10 个元素的生成器类型进行排序?

[英]How do you sort a generator type with 1.72e+10 elements in python according to 2nd "column"?

I have a generator object containing 2 "columns" and 1.72e+10 "rows".我有一个包含 2 个“列”和 1.72e+10 个“行”的生成器对象。 I want to sort the generator object according to the second column, using sorted like this takes an insane amount of time.我想根据第二列对生成器对象进行排序,像这样使用 sorted 需要大量的时间。 I don't know if it makes sense to talk about columns and rows in generators, but I use quotations marks as I don't know what else to call it.我不知道谈论生成器中的列和行是否有意义,但我使用引号,因为我不知道还能怎么称呼它。

sorted_list = sorted(list(generator_obj),
                     key=lambda x: x[1], 
                         reverse=True)

Is there any smart way of sorting a huge generator object quickly?有没有什么聪明的方法可以快速对一个巨大的生成器对象进行排序? I don't have a computer science background, so I guess I am also wondering if it is inevitable to spend a huge amount of time on sorting such a large object?我没有计算机科学背景,所以我想我也想知道是否不可避免地要花费大量时间来对这么大的对象进行排序? Also I believe it is faster to convert the generator_obj before supplying it as an argument for sorted() , which is why I don't submit a raw generator.此外,我相信在将generator_obj作为sorted()的参数提供之前转换它会更快,这就是我不提交原始生成器的原因。

I have also tried to sort in place with the same results as above:我还尝试使用与上述相同的结果进行原位排序:

list(generator_obj).sort(key=lambda x: x[1], reverse=True)

Sorting requires ~N log N time, if all you have is a comparison function.排序需要~N log N时间,如果你只有一个比较函数。

If your keys are integers, you can sort in ~ N time using the radix sort .如果您的键是整数,则可以使用基数 sort~ N时间内进行排序

Either way, sorting a "huge" sequence requires "huge" time.无论哪种方式,对“巨大”序列进行排序都需要“大量”时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何从 Pandas DataFrame 中分组、排序和提取第二大数量? - How do you Group, Sort, and Extract 2nd Highest Quantity from Pandas DataFrame? 你如何在 Python Pandas 中分组、排序和限制? (即获得前 10 名) - How do you group, sort, and limit in Python Pandas? (i.e. Get Top 10) 如何在 python 的 selenium 中使用 xpath 选择具有特定文本的跨度的第二个实例? - How do you select the 2nd instance of a span with a specific text using xpath in python's selenium? 如何在python中对生成器类型进行排序 - How to sort generator type in python 如何在python和C#中通过第二个元组元素对列表进行排序 - How to sort a list by the 2nd tuple element in python and C# 如何首先使用第一列以升序对二维列表进行排序,然后在 python 中以降序对第二列进行排序? - How to sort a 2D list first using 1st column in increasing order, then by 2nd column in decreasing order in python? python 基于列表第二个值的嵌套列表排序在其值为 10 时无法正常工作 - python nested list sort based on 2nd value of the list is not working properly when it has value 10 如何在Python中将列表元素从第2个元素链接到最后一个元素 - How to link list elements from 2nd to last element in Python 如何从 Python 中的元组中获取第二个元素? - How to get 2nd elements from tuples in Python? 如何仅从 Python 的元组中添加第二个元素? - How to add only the 2nd elements from tuples in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM