【发布时间】:2019-06-04 21:57:25
【问题描述】:
当粘贴到命令行 iPython 提示符中时,以下代码
TFN = [True, False, None]
for q in TFN:
gen = (c for c in TFN if c==q)
lcomp = [c for c in TFN if c==q]
print list(gen), "\t",list(lcomp)
...产生:
gen lcomp
[True] [True]
[False] [False]
[None] [None]
但是,当粘贴到 IntelliJ PyCharm iPython 提示符中时,这将变为:
[None] [True]
[None] [False]
[None] [None]
复制:
- 如下图设置断点:
- 选择文件,运行点击,选择“Debug...”
- 等待断点被命中
- 如图所示选择代码。 ALT+SHIFT+E 执行。
第二次你会看到不同的输出!
为什么?
【问题讨论】:
-
您需要提供minimal reproducible example。
TFN是什么?为什么要多次迭代? -
如果不深入了解您正在使用的结构,我们就无法重现问题,也无能为力。
-
我的错。最小示例中缺少的变量丢失了。现已修复。
-
@juanpa.arrivillaga -- 我只在 TFN 上迭代一次。
-
@user48956 不,你不是,你在 for 循环中迭代它,然后 for 循环的每次迭代,你再次迭代它
[c for c in TFN if c==q]...