编辑:这里有一个类似的问题涉及迭代器重置。然而,下面接受的答案解决了嵌套迭代器的实际问题,并处理了一个容易错过的问题,即嵌套迭代器不会重置。
有没有办法在 python 中迭代迭代器两次?
在下面的示例代码中,我可以看到第二次迭代与第一次迭代在同一对象上运行,因此产生了奇怪的结果。将此与下面的 C# 进行对比,得到我想要的结果。
有什么办法可以做我想做的事。我想知道是否可以复制迭代器或“检索”它来自的函数,但也许有一种更简单的方法。(我知道我可以在下面的玩具示例中调用MyIter()两次,但如果我不知道迭代器来自哪里并且不是我想要的,那么这是没有用的!)。
def MyIter():
yield 1;
yield 2;
yield 3;
yield 4;
def PrintCombos(x):
for a in x:
for b in x:
print(a,"-",b);
PrintCombos(MyIter());
Run Code Online (Sandbox Code Playgroud)
给出
1 - 2
1 - 3
1 - 4
Run Code Online (Sandbox Code Playgroud)
对比:
static IEnumerable MyIter()
{
yield return 1;
yield return 2;
yield return 3;
yield return 4;
}
static void PrintCombos(IEnumerable x)
{
foreach (var a in x)
foreach (var b in x)
Console.WriteLine(a + "-" …Run Code Online (Sandbox Code Playgroud) 使用下面的C#代码,您将如何在Visual Basic中编写它?它试图说什么?
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Microsoft.LiveLabs.Pivot
{
/// <summary>
/// Tile Builder class
/// </summary>
public static class TileBuilder
{
/// <summary>
/// Specifies which images are required in the images array used in CreateTile
/// according to the Morton fractal pattern used by Seadragon.
/// </summary>
/// <remarks>
/// Usage of this and CreateTile are kind of tricky. Here's an example:
/// Say you have a results …Run Code Online (Sandbox Code Playgroud) 我不是一个python的家伙,我想了解一些python代码.我想知道下面代码的最后几行是做什么的?这种多个对象是否被返回?还是返回了3个对象的列表?
req = SomeRequestBean()
req.setXXX(xxx)
req.YYY = int(yyy)
device,resp,fault = yield req #<----- What does this mean ?
Run Code Online (Sandbox Code Playgroud) 好吧,当我在构建一个自定义枚举器时,我注意到了这种与收益率有关的行为
说你有这样的事情:
public class EnumeratorExample
{
public static IEnumerable<int> GetSource(int startPoint)
{
int[] values = new int[]{1,2,3,4,5,6,7};
Contract.Invariant(startPoint < values.Length);
bool keepSearching = true;
int index = startPoint;
while(keepSearching)
{
yield return values[index];
//The mind reels here
index ++
keepSearching = index < values.Length;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在技术上从函数返回之后,是什么让编译器的底层可以执行索引++和while循环中的其余代码?
我怎样才能删除这个收益率?我想使用地图而不是:
val cols = for(x <- 0 to 6) yield for(y <- 0 to 5) yield apply(x, y)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
谢谢!
最好的问候,约翰
我刚安装Laravel 4(照亮)和我在浏览器中打开index.php文件,我遇到了这个错误:
解析错误:语法错误,意外的'yield'(T_YIELD),期望标识符(T_STRING)在第339行/www/Laravel4/vendor/illuminate/view/src/Illuminate/View/Environment.php
我已经修复了元文件夹的权限,并通过Composer安装了所有依赖项.我在OSX 10.8.2上运行PHP版本5.5.0alpha2.
以下代码的工作正确:
def file_gen(f_name):
f = open(f_name)
for line in f:
yield line
gen_line = file_gen("foo.html")
gen_line.next() # '<!DOCTYPE>\n'
gen_line.next() # '<html> \n'
gen_line.next() # ... next line in file
Run Code Online (Sandbox Code Playgroud)
但是这个功能提升了StopIteration.我不明白为什么?
def file_gen(f_name):
f = open(f_name)
line = f.readline()
yield line
gen_line = file_gen('foo.html')
gen_line.next() # '<!DOCTYPE>\n'
gen_line.next() # StopIteration
Run Code Online (Sandbox Code Playgroud) 我一直在Ruby中编写相同的代码模式,看起来它会受益于'do'样式的代码,但我不确定如何编写该方法.
我一直在做这种代码模式,它以相同的代码行开始和结束......
x.increment!(:step_count) # same each time
# ...then some different code each
x.update_column(:step_description, "blerg message") # same each time
Run Code Online (Sandbox Code Playgroud)
我觉得它会受益于'做'这样的东西......
update_steps "blerg message" do
# ...then some different code each
end
Run Code Online (Sandbox Code Playgroud)
然后在'do'内部每次执行公共代码.
我将如何制作一个可以使用'do'的方法.
谢谢!
编辑:我认为不要关闭它是很重要的,因为我不知道要搜索'block'或'yield'.可能不知道这些术语的人最终可能会搜索"do".
我在MDN上偶然发现了生成器函数,让我感到困惑的是以下示例:
function* logGenerator() {
console.log(yield);
console.log(yield);
console.log(yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next();
gen.next('pretzel'); // pretzel
gen.next('california'); // california
gen.next('mayonnaise'); // mayonnaise
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么作为yield参数的语句console.log返回传递给.next()生成器方法的参数.这是否发生是因为空yield必须返回.next()方法的第一个参数的值?
我还尝试了一些例子,似乎证实了上述陈述:
gen.next(1,2,3); // the printed value is 1, the 2 and 3 are ignored
// and the actual yielded value is undefined
Run Code Online (Sandbox Code Playgroud)
还有一种方法可以访问.next() …
我正在尝试创建一个自定义数据生成器,但不知道如何将yield函数与__getitem__方法内部的无限循环结合起来。
编辑:回答后我意识到我使用的代码是Sequence不需要yield声明的。
目前我正在返回多个带有return声明的图像:
class DataGenerator(tensorflow.keras.utils.Sequence):
def __init__(self, files, labels, batch_size=32, shuffle=True, random_state=42):
'Initialization'
self.files = files
self.labels = labels
self.batch_size = batch_size
self.shuffle = shuffle
self.random_state = random_state
self.on_epoch_end()
def __len__(self):
return int(np.floor(len(self.files) / self.batch_size))
def __getitem__(self, index):
# Generate indexes of the batch
indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]
files_batch = [self.files[k] for k in indexes]
y = [self.labels[k] for k in indexes]
# Generate …Run Code Online (Sandbox Code Playgroud) yield ×10
python ×4
generator ×3
c# ×2
enumeration ×1
for-loop ×1
iterator ×1
javascript ×1
keras ×1
laravel ×1
loops ×1
php ×1
ruby ×1
scala ×1
syntax ×1
tensorflow ×1
vb.net ×1
yield-return ×1