这个功能有什么作用? (Python迭代器) [英] What does this function do? (Python iterators)

查看:132
本文介绍了这个功能有什么作用? (Python迭代器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def partition(n, iterable):
   p = izip_longest(*([iter(iterable)] * n))
   r = []
   for x in p:
       print(x) #I added this
       s = set(x)
       s.discard(None)
       r.append(list(s))
   return r

这实际上是在SO上的一个职位发布并且是一个新手我认为它很有意思所以你得到如下输出:

This is actually in a job posting on SO and being a newbie I thought it was interesting. So you get output like the following:

partition(5, L)
(1, 2, 3, 4, None)
Out[86]: [[1, 2, 3, 4]]



<对我来说,这已经令人困惑,因为我认为 izip_longest(*([iter(iterable)] * n))将运行 izip_longest 函数在n个相同迭代器的列表上,所以我本来期望首先输出(1,1,1,1,1)然后输出(2,2,2,2,2)等等。

To me this is already confusing because I thought izip_longest(*([iter(iterable)] * n))would run the izip_longest function on a list of n identical iterators so I would have expected first an output of (1,1,1,1,1) and then an output of (2,2,2,2,2) and so on.

我的问题这么简短的版本,这就是这一行:

So short version of my question, is what's going on with this line:

 p = izip_longest(*([iter(iterable)] * n))

解析它我会想[iter(iterable)] * n创建一个长度为n的列表迭代所有指向相同的东西 - 这就是它在命令行上所做的事情,但这似乎不是它在这里根据上面打印的输出所做的。

Parsing it I would have thought [iter(iterable)]*n creates a list of length n of identical iterables all pointing to the same thing - that's what it does on the command line, but that doesn't seem to be what it does here based on the output printed above.

此外,我认为*开头的* ...最长(* ... 自列表以来是不确定的长度,但我认为这完全没有意义。在函数调用内部第一个 * 符号是什么?似乎它只是表示一个未知的参数长度...

Also I thought the * at the beginning ...longest(*... was there since the list is of unknown length but I don't think that entirely makes sense. What is that first * symbol doing inside the function call? Doesn't seem like it's simply indicating an unknown length list of arguments...

所以在一天结束的时候我完全迷失了。有人可以告诉我这个语法吗?

So at the end of the day I'm completely lost. Can someone walk me through this syntax?

非常感谢任何输入!

感谢所有有用的答案,每个人。我不确定我是否正在寻找答案或问题,但在我看来,这个列表理解将对列表和元组做同样的事情(我意识到迭代器也适用于字典,自定义类,其他东西.. 。)

Thanks for all the helpful answers, everyone. I am not sure if I am tacking on an answer or a question here, but it seems to me this list comprehension will do the same thing for lists and tuples (I realize iterators would also apply to dictionaries, custom classes, other things...)

[L[i*n:(i+1)*n] for i in range(int(ceil(len(L)/float(n)))) ]


推荐答案

鉴于:

>>> li
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

使用 zip iter * operator 将列表分区为n长度列表:

There is a common Python idiom of using zip in combination with iter and * operator to partition a list a flat list into a list of lists of n length:

>>> n=3
>>> zip(*([iter(li)] * n))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14), (15, 16, 17), (18, 19, 20)]

但是,如果 n 不是总长度的偶数倍,则最终列表将被截断:

However, if n is not an even multiple of the overall length, the final list is truncated:

>>> n=4
>>> zip(*([iter(li)] * n))
[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15), (16, 17, 18, 19)]

你可以使用 izip_longest 来使用填写了所选内容的完整列表不完整子列表的值:

You can use izip_longest to use the complete list filled in with a selected value for the incomplete sub lists:

>>> list(izip_longest(*([iter(li)] * n)))
[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15), (16, 17, 18, 19), (20, None, None, None)]

这篇关于这个功能有什么作用? (Python迭代器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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