700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python排序输出_如何在python中使用排序输出进行izip呢?

python排序输出_如何在python中使用排序输出进行izip呢?

时间:2023-01-07 00:52:17

相关推荐

python排序输出_如何在python中使用排序输出进行izip呢?

由于没有更好的名称,我想用Python编写一个“izip_sorted”。函数的输入是许多iterable,每一个都是排序的。输出是具有排序输出的单个iterable。在print([x for x in izip_sorted([0,4,8], [1,3,5], [12,12,42],[])])

编辑:这是一个简单的例子。真正的使用将是在大约40个输入ITerable上,每个都有大约100000个元素。每个元素都是一个类,存储一个dict并实现__cmp__,以便对元素进行排序。数据太大,不能一次全部读入。在

应打印

^{pr2}$

我有一个解决方案,但我对python还不太熟悉,我不知道它是不是很像python。这能改进吗?只有一个元素改变的排序看起来很浪费。。。在def izip_sorted(*iterables):

"""

Return an iterator that outputs the values from the iterables, in sort order

izip_sort('ABF', 'D', 'CE') --> A B C D E F

"""

iterators = [iter(it) for it in iterables]

current_iterators = []

for it in iterators:

try:

current_iterators.append((next(it), it))

except StopIteration:

pass

current_iterators.sort(key=lambda x: x[0])

while(current_iterators):

yield current_iterators[0][0]

try:

current_iterators[0] = (next(current_iterators[0][1]), current_iterators[0][1])

current_iterators.sort(key=lambda x: x[0])

except StopIteration:

current_iterators = current_iterators[1:]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。