列表中的对 [英] pairs from a list

查看:45
本文介绍了列表中的对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表中生成顺序对。

这是一种方式::


来自itertools import izip,islice

for xz in izip(islice(x,0,None,2),islice(x,1,None,2)):

print x12


(当然打印声明只是说明性的。)

最快的方法是什么? (忽略导入时间。)


谢谢,

Alan Isaac

解决方案

< blockquote> Alan Isaac< ai **** @ american.eduwrites:


(当然打印声明只是说明性的。)

最快的方法是什么? (忽略导入时间。)



你必须尝试一些不同的方式并计时。一个

的想法(未经测试):


def对(seq):

而True:

yield(seq.next(),seq.next())


1月21日晚上10点20分,Alan Isaac< ais ... @ american。 eduwrote:


我想从列表中生成顺序对。

这是一种方式::


来自itertools import izip,islice

for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)):

打印x12


(当然打印声明只是说明性的。)

最快的方法是什么? (忽略导入时间。)



查找timeit模块并测试各种替代方案;

这是最可靠的告诉你肯定的方式。


乔治


1月22日凌晨3点20分,Alan Isaac< ais .. 。@ american.eduwrote:


我想从列表中生成顺序对。



<< snip>>


最快的方法是什么? (忽略导入时间。)



1)你的方法有多快?

2)它需要多快是否适用于您的申请?

3)您的申请中是否存在其他任何瓶颈?

4)这是一个例程,其最小的加速百分比将给出

最大的整体申请速度加快?


- Paddy。


I want to generate sequential pairs from a list.
Here is a way::

from itertools import izip, islice
for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)):
print x12

(Of course the print statement is just illustrative.)
What is the fastest way? (Ignore the import time.)

Thanks,
Alan Isaac

解决方案

Alan Isaac <ai****@american.eduwrites:

(Of course the print statement is just illustrative.)
What is the fastest way? (Ignore the import time.)

You have to try a bunch of different ways and time them. One
idea (untested):

def pairs(seq):
while True:
yield (seq.next(), seq.next())


On Jan 21, 10:20 pm, Alan Isaac <ais...@american.eduwrote:

I want to generate sequential pairs from a list.
Here is a way::

from itertools import izip, islice
for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)):
print x12

(Of course the print statement is just illustrative.)
What is the fastest way? (Ignore the import time.)

Look up the timeit module and test yourself the various alternatives;
that''s the most reliable way to tell for sure.

George


On Jan 22, 3:20 am, Alan Isaac <ais...@american.eduwrote:

I want to generate sequential pairs from a list.

<<snip>>

What is the fastest way? (Ignore the import time.)

1) How fast is the method you have?
2) How much faster does it need to be for your application?
3) Are their any other bottlenecks in your application?
4) Is this the routine whose smallest % speed-up would give the
largest overall speed up of your application?

- Paddy.


这篇关于列表中的对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆