Data.List.iterate懒惰评估没有发生(Data.List.iterate lazy evaluation not happening)
我有上述功能,但是当我调用它时,它会卡住,Data.List.iterate会在不停止的情况下进行评估。
rp:: RandomGen g => g -> ([Int], g) rp g = (map (\x -> (last (fst x))) lst , snd (next g)) where lst = (Data.List.iterate id ([1], g_second)) (g_first, g_second) = (split g)
为什么会这样?
谢谢!
I'm having the above function, but when I call it, it gets stuck, the Data.List.iterate evaluates without stopping.
rp:: RandomGen g => g -> ([Int], g) rp g = (map (\x -> (last (fst x))) lst , snd (next g)) where lst = (Data.List.iterate id ([1], g_second)) (g_first, g_second) = (split g)
Why does that happend?
Thank you!
原文:https://stackoverflow.com/questions/36726046
最满意答案
使用附加的状态变量,例如索引变量(通常用于C或PHP语言),被认为是非pythonic。
更好的选择是使用内置函数
enumerate()
,可用于Python 2和3:for idx, val in enumerate(ints): print(idx, val)
查看PEP 279了解更多。
Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic.
The better option is to use the built-in function
enumerate()
, available in both Python 2 and 3:for idx, val in enumerate(ints): print(idx, val)
Check out PEP 279 for more.
相关问答
更多-
tldr,再次; CoffeeScript的作者告诉我, 我是对的 :不要使用_i 。 14:29
You shouldn't use internal variables. ... 14:42 I was hoping something more deeply involved in the language would be able to put some authority behind that opinion 14:43 ... ... -
使用附加的状态变量,例如索引变量(通常用于C或PHP语言),被认为是非pythonic。 更好的选择是使用内置函数enumerate() ,可用于Python 2和3: for idx, val in enumerate(ints): print(idx, val) 查看PEP 279了解更多。 Using an additional state variable, such as an index variable (which you would normally use in languag ...
-
一个简单的方法是使用python的切片符号 - 请参阅基本教程中的部分。 https://docs.python.org/2/tutorial/introduction.html for item in ints[1:]: 这将迭代你的列表跳过第0个元素。 由于您实际上并不需要索引,因此该实现比不必要地维护索引的实现更清晰 A simple way is to use python's slice notation - see the section in the basic tutorial. https ...
-
前两个基本相同,因为它们将在各种情况下运行相同数量的迭代,并且将index保持在相同的状态。 如果用index.append(a+1)替换index.append(a+1) index.append(a) , for循环将永远运行; 同样,如果用index.append(index[-1] + 1)替换index.append(index[-1] + 1) index.append(index[-1]) , while循环将永远运行。 修改一个你正在迭代的列表有点令人担忧,但它有时是一种合理的策略。 顺便说 ...
-
如果我理解你要做什么,我不确定你需要count和count2 。 我想你可以使用循环生成的数字。 我建议使用enumerate()而不是range(len()) 。 for i1,rlength1 in enumerate(records): ex = rlength1.id for i2,rlength2 in enumerate(records): if re.search(ex, rlength2.id) != None: print rleng ...
-
编辑 在你重大改变的批次(实际上是一个新问题)改变 call echo !input[%x%]! 至 call echo %%input[!x!]%% 如果在(代码块)中,您可能需要延迟扩展来强制实际值,如果同时使用索引变量,则需要另一个级别的延迟扩展,您可以使用伪调用完成,并将百分号加倍 Call set Input=%%input[!x!]%% mediainfo --Output=Video;%%Height%% !input! > %temp%\ffmpegres%x%.txt EDIT ...
-
多个嵌套循环索引(multiple nested loops index)[2024-03-08]
通常,您有一个嵌套的总和: sum(sum(sum(1,i3=0..i2),i2=0..i1),i1=0..n-1) 你可以使用这些基本的求和公式从内到外简化。 最终结果将仅依赖于i作为izomorphius评论,因为N是i1的上限,i1是i2的上限,... 编辑:好的,通过编辑,我现在看到你有一个不同的问题。 现在它有点复杂但仍然没有问题。 我们需要为此分割一些东西。 我将计算值k = f(4,3,1)。 在那个时间点,我们将执行4个完整的i1循环(i1 = 0,1,2,3)并且正在执行第五个循环。 可 ... -
我将此作为第二个答案添加,因为它使用的是另一种语言(现在为C),并且具有更直接的方法。 我保留原来的答案,因为如果没有它,下面的代码几乎是无法解释的。 我将我的两个函数合并为一个函数来减少函数调用开销。 另外,为了100%确定它回答了原始问题,我逐字地使用了该问题的循环。 在驱动程序函数中,我明确地显示输出对于N = 4是正确的,然后对N = 10000进行压力测试(总共100,000,000次通过内部循环)。 我没有任何正式的计时代码,但我的机器需要大约1秒才能运行并测试这些1亿个案例。 我的代码假定为3 ...
-
如果您希望内部部件是列表,那么您可以将enumerate结果转换为列表理解中的列表: >>> mylist = ["aus","ausser","bei","mit","noch","seit","von","zu"] >>> [[idx, item] for idx, item in enumerate(mylist, 1)] [[1, 'aus'], [2, 'ausser'], [3, 'bei'], [4, 'mit'], [5, 'noch'], [6, 'seit'], [7, 'v ...
-
你说element3我假设你的意思是element2 for (var i = 0; i < 2; i++) { console.log('i:', i); ["element0", "element1", "element2"].forEach(function (element, index) { console.log('element:', element, 'index:', i); }); } 你使用i作为循环计数器和forEach索引 you said element3 i'm as ...