- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
从这里开始: https://docs.python.org/3/library/itertools.html#itertools-recipes
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 zip_longest(*args, fillvalue=fillvalue)
我理解 zip_longest
调用。但我没有得到:
args = [iter(iterable)] * n
如果稍后要将可迭代对象传递到 izip_longest
中,为什么还要再次将可迭代对象包装到 iter()
中呢?我不能这样做吗:
args = [iterable] * n
但似乎没有iter()
,它只是将同一个迭代器重复n次。但是把它放在 iter()
中如何改变它的行为呢?
最佳答案
这种分组利用了迭代器的单次传递特性(与单纯的可迭代对象相反,它可以潜在地迭代多次,并且在非迭代器可迭代对象上使用 iter
应该返回一个新的独立迭代器。相比之下,在迭代器上调用 iter
返回迭代器本身。
所以这是一个只接受两个参数的 zip 函数的简单实现:
In [1]: def myzip(x, y):
...: itx, ity = iter(x), iter(y)
...: while True:
...: try:
...: a, b = next(itx), next(ity)
...: except StopIteration:
...: return
...: yield a, b
...:
In [2]: list(zip('abcd','efgh'))
Out[2]: [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', 'h')]
In [3]: list(myzip('abcd','efgh'))
Out[3]: [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', 'h')]
这几乎就是内置 zip
的工作原理。现在,如果我们将列表作为可迭代项来执行上述操作会怎么样?
In [16]: mylist = [1,2,3,4]
In [17]: iterable = mylist
In [18]: itx, ity = iter(iterable), iter(iterable)
In [19]: itx is ity
Out[19]: False
In [20]: next(itx), next(ity)
Out[20]: (1, 1)
In [21]: next(itx), next(ity)
Out[21]: (2, 2)
In [22]: next(itx), next(ity)
Out[22]: (3, 3)
In [23]: next(itx), next(ity)
Out[23]: (4, 4)
In [24]: next(itx), next(ity)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-24-b6cbb26d280f> in <module>()
----> 1 next(itx), next(ity)
StopIteration:
但是,如果 iterable
是一个迭代器:
In [25]: iterable = iter(mylist)
In [26]: itx, ity = iter(iterable), iter(iterable)
In [27]: itx is ity
Out[27]: True
In [28]: next(itx), next(ity)
Out[28]: (1, 2)
In [29]: next(itx), next(ity)
Out[29]: (3, 4)
In [30]: next(itx), next(ity)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-30-b6cbb26d280f> in <module>()
----> 1 next(itx), next(ity)
StopIteration:
最后,请注意序列上的 repition 永远不会复制序列的元素,因此执行 [iter(x)]*n
会返回一个列表,其中包含 n 个对同一迭代器的引用,因此:
In [32]: args = [iter(mylist)]*3
In [33]: args
Out[33]:
[<list_iterator at 0x1040c9320>,
<list_iterator at 0x1040c9320>,
<list_iterator at 0x1040c9320>]
注意,它们是相同的 list_iterator
对象...
关于python - itertools "grouper"函数配方如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49180862/
我正在从数据库中提取元素详细信息,并编写一个动态 HTML 页面以在顶部打印这些元素详细信息。该页面的目的是在团队或其他 session 中记录有关元素的信息,我希望在元素详细信息之后的空间中出现线条
我正在开发一个涉及 native 代码的 android 项目,我正在尝试将 Kiss FFT 库与 NDK 一起使用。但是,我很难理解 Kiss FFT 使用的 makefile 下面列出的部分。谁
您可以通过brew install package1 package2安装多个自制程序公式。但是,如果您有一个文本文件,其中包含要安装的所有软件包。你会怎么做? brew install < pack
在尝试通过 Homebrew 安装 FreeTDS 时,我运行了 brew edit freetds .我编辑了文件。我想将 Recipe 文件 (freetds.rb) 恢复到它的原始状态,以防我在
今天我通过 homebrew 安装了 curl 公式,但在安装它(并重新采购 shell)后我注意到: % which curl /usr/bin/curl 事实上,brew install curl
我正在尝试用我自己的包构建 yocto 图像。我在 github 上有使用 cmake 的 OpenCV 代码。 我试图为其编写一个食谱,但遇到了很多错误。任何人都可以提示我的食谱中应包含哪些功能或参
有谁知道如何在食谱上使用此处文档重定向? test: sh <
我目前正在玩柯南。所以我创建了两个小项目:第一个项目是一个小型库“fcdk”:https://github.com/lmarzull/fcdk/tree/devel 第二个是一个包含 conan 和
我正在尝试制作一个 brew我的大学特有的编译器公式。 该软件包没有./configure , make , make install安装方式。相反,它只是要求您有 java和javac安装并执行以下
我在执行 procmail 配方时遇到了一些问题。 这是我到目前为止得到的: :0 * ^X-Loop: myemail@gmail\.com /dev/null :
我正在为基于 Makefile 的项目的 Bitbake 编写自定义配方。我们能够使用包中的所有文件创建 RPM,但我们无法找到安装前和安装后操作的方法。由于应用程序作为服务运行,我们希望在预安装步骤
我正准备为我正在开发的游戏开发我的第一个 Windows 安装程序。我听说过 NSIS 推荐,但我的需求很基本,我无法弄清楚到底需要什么。 我想要一个简短的 NSIS 脚本,它可以在“Program
我想在 Shiny 面板中显示 -LaTeX 格式的公式,但我找不到组合 textOutput 的方法与 withMathJax .我尝试了以下但没有奏效。任何帮助将不胜感激。 --ui.r ...
我正在尝试弄清楚如何说服 Yocto 使用我的本地 GIT 源而不是它使用的标准位置来构建 gcc。默认情况下,构建 GCC 的配方位于 meta/recipes-devtools/gcc/gcc_5
我在我正在编写的一些代码中看到了这种模式 e = {...} # a dictionary e["table"] = "users" e["timestamp"] = time.time() queu
我正在使用 Petalinux 工具生成 Linux 镜像。我已经为 PostgreSQL 添加了一个现有的配方。 我怎样才能改变配方,使数据位置不是/var/lib/postgresql,而是/va
我现在遇到了一个真正让我发疯的问题。我有一个用 C++ 编写的项目,它正在使用 pthread。我昨天用 Dev-C 编译了这个项目,一切都很完美。然而,今天,我收到了这个毫无意义的“目标‘proje
我正在开发一个食谱应用程序来帮助我妻子培养她的蛋糕爱好。这个想法是创建一个食谱数据库来保存她所有的蛋糕食谱。 每个食谱都有多种成分。每种成分都会有测量值(克、毫升、茶匙等),然后是数量。 我了解如何创
我想在图像构建过程中将文件夹及其内容复制到 yocto。对于这个过程,我使用以下配方 SUMMARY = "Installation Recipe" DESCRIPTION = "It install
我已经为 IFTTT channel 创建了触发器和操作。现在我想使用这些触发器和操作创建一个食谱,但我不想通过 Maker,而是使用 API 调用。用于创建食谱的 API 调用(在 Maker UI
我是一名优秀的程序员,十分优秀!