问题:一对单对
通常,我发现需要成对处理列表。我想知道哪种方法是有效的pythonic方法,并在Google上找到了它:
pairs = zip(t[::2], t[1::2])
我认为这已经足够好用了,但是在最近涉及成语与效率的讨论之后,我决定进行一些测试:
import time
from itertools import islice, izip
def pairs_1(t):
return zip(t[::2], t[1::2])
def pairs_2(t):
return izip(t[::2], t[1::2])
def pairs_3(t):
return izip(islice(t,None,None,2), islice(t,1,None,2))
A = range(10000)
B = xrange(len(A))
def pairs_4(t):
# ignore value of t!
t = B
return izip(islice(t,None,None,2), islice(t,1,None,2))
for f in pairs_1, pairs_2, pairs_3, pairs_4:
# time the pairing
s = time.time()
for i in range(1000):
p = f(A)
t1 = time.time() - s
# time using the pairs
s = time.time()
for i in range(1000):
p = f(A)
for a, b in p:
pass
t2 = time.time() - s
print t1, t2, t2-t1
这些是我计算机上的结果:
1.48668909073 2.63187503815 1.14518594742
0.105381965637 1.35109519958 1.24571323395
0.00257992744446 1.46182489395 1.45924496651
0.00251388549805 1.70076990128 1.69825601578
如果我正确地解释了它们,那应该意味着在Python中实现列表,列表索引和列表切片非常有效。这是令人安慰和意外的结果。
是否有另一种“更好”的成对遍历列表的方式?
请注意,如果列表中元素的数量为奇数,则最后一个元素将不在任何对中。
确保包含所有元素的正确方法是哪种?
我从测试答案中添加了这两个建议:
def pairwise(t):
it = iter(t)
return izip(it, it)
def chunkwise(t, size=2):
it = iter(t)
return izip(*[it]*size)
结果如下:
0.00159502029419 1.25745987892 1.25586485863
0.00222492218018 1.23795199394 1.23572707176
到目前为止的结果
最pythonic,非常高效:
pairs = izip(t[::2], t[1::2])
最有效且非常pythonic:
pairs = izip(*[iter(t)]*2)
我花了一点时间想知道第一个答案使用了两个迭代器,而第二个答案使用了一个迭代器。
为了处理具有奇数个元素的序列,建议增加原始序列,增加一个元素(None
)与之前的最后一个元素配对,这可以通过实现itertools.izip_longest()
。
最后
请注意,在Python 3.x中,zip()
其行为与itertools.izip()
并itertools.izip()
消失了。
Often enough, I’ve found the need to process a list by pairs. I was wondering which would be the pythonic and efficient way to do it, and found this on Google:
pairs = zip(t[::2], t[1::2])
I thought that was pythonic enough, but after a recent discussion involving idioms versus efficiency, I decided to do some tests:
import time
from itertools import islice, izip
def pairs_1(t):
return zip(t[::2], t[1::2])
def pairs_2(t):
return izip(t[::2], t[1::2])
def pairs_3(t):
return izip(islice(t,None,None,2), islice(t,1,None,2))
A = range(10000)
B = xrange(len(A))
def pairs_4(t):
# ignore value of t!
t = B
return izip(islice(t,None,None,2), islice(t,1,None,2))
for f in pairs_1, pairs_2, pairs_3, pairs_4:
# time the pairing
s = time.time()
for i in range(1000):
p = f(A)
t1 = time.time() - s
# time using the pairs
s = time.time()
for i in range(1000):
p = f(A)
for a, b in p:
pass
t2 = time.time() - s
print t1, t2, t2-t1
These were the results on my computer:
1.48668909073 2.63187503815 1.14518594742
0.105381965637 1.35109519958 1.24571323395
0.00257992744446 1.46182489395 1.45924496651
0.00251388549805 1.70076990128 1.69825601578
If I’m interpreting them correctly, that should mean that the implementation of lists, list indexing, and list slicing in Python is very efficient. It’s a result both comforting and unexpected.
Is there another, “better” way of traversing a list in pairs?
Note that if the list has an odd number of elements then the last one will not be in any of the pairs.
Which would be the right way to ensure that all elements are included?
I added these two suggestions from the answers to the tests:
def pairwise(t):
it = iter(t)
return izip(it, it)
def chunkwise(t, size=2):
it = iter(t)
return izip(*[it]*size)
These are the results:
0.00159502029419 1.25745987892 1.25586485863
0.00222492218018 1.23795199394 1.23572707176
Results so far
Most pythonic and very efficient:
pairs = izip(t[::2], t[1::2])
Most efficient and very pythonic:
pairs = izip(*[iter(t)]*2)
It took me a moment to grok that the first answer uses two iterators while the second uses a single one.
To deal with sequences with an odd number of elements, the suggestion has been to augment the original sequence adding one element (None
) that gets paired with the previous last element, something that can be achieved with itertools.izip_longest()
.
Finally
Note that, in Python 3.x, zip()
behaves as itertools.izip()
, and itertools.izip()
is gone.
回答 0
我最喜欢的方式:
from itertools import izip
def pairwise(t):
it = iter(t)
return izip(it,it)
# for "pairs" of any length
def chunkwise(t, size=2):
it = iter(t)
return izip(*[it]*size)
当您要配对所有元素时,您显然可能需要一个fillvalue:
from itertools import izip_longest
def blockwise(t, size=2, fillvalue=None):
it = iter(t)
return izip_longest(*[it]*size, fillvalue=fillvalue)
My favorite way to do it:
from itertools import izip
def pairwise(t):
it = iter(t)
return izip(it,it)
# for "pairs" of any length
def chunkwise(t, size=2):
it = iter(t)
return izip(*[it]*size)
When you want to pair all elements you obviously might need a fillvalue:
from itertools import izip_longest
def blockwise(t, size=2, fillvalue=None):
it = iter(t)
return izip_longest(*[it]*size, fillvalue=fillvalue)
回答 1
我想说您的初始解决方案pairs = zip(t[::2], t[1::2])
是最好的解决方案,因为它最容易阅读(在Python 3中,它会zip
自动返回一个迭代器而不是列表)。
为了确保包括所有元素,您可以通过扩展列表None
。
然后,如果列表中元素的数量为奇数,则最后一对将为(item, None)
。
>>> t = [1,2,3,4,5]
>>> t.append(None)
>>> zip(t[::2], t[1::2])
[(1, 2), (3, 4), (5, None)]
>>> t = [1,2,3,4,5,6]
>>> t.append(None)
>>> zip(t[::2], t[1::2])
[(1, 2), (3, 4), (5, 6)]
I’d say that your initial solution pairs = zip(t[::2], t[1::2])
is the best one because it is easiest to read (and in Python 3, zip
automatically returns an iterator instead of a list).
To ensure that all elements are included, you could simply extend the list by None
.
Then, if the list has an odd number of elements, the last pair will be (item, None)
.
>>> t = [1,2,3,4,5]
>>> t.append(None)
>>> zip(t[::2], t[1::2])
[(1, 2), (3, 4), (5, None)]
>>> t = [1,2,3,4,5,6]
>>> t.append(None)
>>> zip(t[::2], t[1::2])
[(1, 2), (3, 4), (5, 6)]
回答 2
我从小的免责声明开始-不要使用下面的代码。根本不是Pythonic,我只是为了好玩而写。它类似于@ THC4k pairwise
函数,但使用iter
和lambda
闭包。它不使用itertools
模块,不支持fillvalue
。我把它放在这里是因为有人可能会觉得有趣:
pairwise = lambda t: iter((lambda f: lambda: (f(), f()))(iter(t).next), None)
I start with small disclaimer – don’t use the code below. It’s not Pythonic at all, I wrote just for fun. It’s similar to @THC4k pairwise
function but it uses iter
and lambda
closures. It doesn’t use itertools
module and doesn’t support fillvalue
. I put it here because someone might find it interesting:
pairwise = lambda t: iter((lambda f: lambda: (f(), f()))(iter(t).next), None)
回答 3
就大多数pythonic而言,我想说python源文档中提供的食谱(其中一些看起来很像@JochenRitzel提供的答案)可能是您最好的选择;)
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
As far as most pythonic goes, I’d say the recipes supplied in the python source docs (some of which look a lot like the answers that @JochenRitzel provided) is probably your best bet ;)
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
On modern python you just have to use zip_longest(*args, fillvalue=fillvalue)
according to the corresponding doc page.
回答 4
是否有另一种“更好”的成对遍历列表的方式?
我不能肯定地说,但我对此表示怀疑:任何其他遍历都会包含更多必须解释的Python代码。诸如zip()之类的内置函数是用C编写的,这要快得多。
确保包含所有元素的正确方法是哪种?
检查列表的长度,如果它是奇数(len(list) & 1 == 1
),则复制列表并附加一个项目。
Is there another, “better” way of traversing a list in pairs?
I can’t say for sure but I doubt it: Any other traversal would include more Python code which has to be interpreted. The built-in functions like zip() are written in C which is much faster.
Which would be the right way to ensure that all elements are included?
Check the length of the list and if it’s odd (len(list) & 1 == 1
), copy the list and append an item.
回答 5
>>> my_list = [1,2,3,4,5,6,7,8,9,10]
>>> my_pairs = list()
>>> while(my_list):
... a = my_list.pop(0); b = my_list.pop(0)
... my_pairs.append((a,b))
...
>>> print(my_pairs)
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> my_list = [1,2,3,4,5,6,7,8,9,10]
>>> my_pairs = list()
>>> while(my_list):
... a = my_list.pop(0); b = my_list.pop(0)
... my_pairs.append((a,b))
...
>>> print(my_pairs)
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
回答 6
只做:
>>> l = [1, 2, 3, 4, 5, 6]
>>> [(x,y) for x,y in zip(l[:-1], l[1:])]
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
Only do it:
>>> l = [1, 2, 3, 4, 5, 6]
>>> [(x,y) for x,y in zip(l[:-1], l[1:])]
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
回答 7
这是使用生成器创建对/腿的示例。生成器不受堆栈限制
def pairwise(data):
zip(data[::2], data[1::2])
例:
print(list(pairwise(range(10))))
输出:
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
Here is an example of creating pairs/legs by using a generator. Generators are free from stack limits
def pairwise(data):
zip(data[::2], data[1::2])
Example:
print(list(pairwise(range(10))))
Output:
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
回答 8
万一有人需要明智的答案算法,这里是:
>>> def getPairs(list):
... out = []
... for i in range(len(list)-1):
... a = list.pop(0)
... for j in a:
... out.append([a, j])
... return b
>>>
>>> k = [1, 2, 3, 4]
>>> l = getPairs(k)
>>> l
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
但是请注意,您原来的列表也将被简化为最后一个元素,因为您使用pop
了它。
>>> k
[4]
Just in case someone needs the answer algorithm-wise, here it is:
>>> def getPairs(list):
... out = []
... for i in range(len(list)-1):
... a = list.pop(0)
... for j in a:
... out.append([a, j])
... return b
>>>
>>> k = [1, 2, 3, 4]
>>> l = getPairs(k)
>>> l
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
But take note that your original list will also be reduced to its last element, because you used pop
on it.
>>> k
[4]