如果我有一个IEnumerable像:
string[] items = new string[] { "a", "b", "c", "d" };
Run Code Online (Sandbox Code Playgroud)
我想循环通过所有连续项目(大小为2的滑动窗口).这将是
("a","b"), ("b", "c"), ("c", "d")
Run Code Online (Sandbox Code Playgroud)
我的解决方案就是这样
public static IEnumerable<Pair<T, T>> Pairs(IEnumerable<T> enumerable) {
IEnumerator<T> e = enumerable.GetEnumerator(); e.MoveNext();
T current = e.Current;
while ( e.MoveNext() ) {
T next = e.Current;
yield return new Pair<T, T>(current, next);
current = next;
}
}
// used like this :
foreach (Pair<String,String> pair in IterTools<String>.Pairs(items)) {
System.Out.PrintLine("{0}, {1}", pair.First, pair.Second)
}
Run Code Online (Sandbox Code Playgroud)
当我编写这段代码时,我想知道.NET框架中是否已经存在执行相同操作的函数,并且它不仅适用于对,而且适用于任何大小的元组.恕我直言应该有一个很好的方法来做这种滑动窗口操作.
我使用C#2.0,我可以想象使用C#3.0(w/LINQ)有更多(更好)的方法来做到这一点,但我主要对C#2.0解决方案感兴趣.不过,我也很欣赏C#3.0解决方案.
我可能以错误的方式解决这个问题,但我想知道如何在python中处理这个问题.
首先是一些c代码:
int i;
for(i=0;i<100;i++){
if(i == 50)
i = i + 10;
printf("%i\n", i);
}
Run Code Online (Sandbox Code Playgroud)
好的,所以我们永远不会看到50年代......
我的问题是,如何在python中做类似的事情?例如:
for line in cdata.split('\n'):
if exp.match(line):
#increment the position of the iterator by 5?
pass
print line
Run Code Online (Sandbox Code Playgroud)
由于我在python方面的经验有限,我只有一个解决方案,介绍一个计数器和另一个if语句.在exp.match(line)为真之后,打破循环直到计数器达到5.
必须有一个更好的方法来做到这一点,希望是一个不涉及导入另一个模块的方法.
提前致谢!
Iterator和Iterablescala有什么区别?
我认为这Iterable代表了一个我可以迭代的集合,并且Iterator是可迭代集合中某个项目的"指针".
然而,Iterator有一个像功能forEach,map,foldLeft.它可以转换为Iterablevia toIterable.而且,例如,scala.io.Source.getLines退货Iterator,而不是Iterable.
但我不能做groupBy的Iterator,我能做到这一点的Iterable.
那么,什么是这两个之间的关系,Iterator和Iterable?
我理解函数如何在for循环中使用range()和zip()可以使用.但是我希望range()输出一个列表 - 就像seq在unix shell中一样.如果我运行以下代码:
a=range(10)
print(a)
Run Code Online (Sandbox Code Playgroud)
输出是range(10),表明它不是列表而是不同类型的对象.zip()打印时有类似的行为,输出类似的东西
<zip object at "hexadecimal number">
Run Code Online (Sandbox Code Playgroud)
所以我的问题是它们是什么,有什么优势使它们成为现实,如何在没有循环的情况下将它们的输出发送到列表?
例如,Python中的文件是可迭代的 - 它们遍历文件中的行.我想计算行数.
一个快速的方法是这样做:
lines = len(list(open(fname)))
Run Code Online (Sandbox Code Playgroud)
但是,这会将整个文件加载到内存中(一次).这相当违背了迭代器的目的(它只需要将当前行保留在内存中).
这不起作用:
lines = len(line for line in open(fname))
Run Code Online (Sandbox Code Playgroud)
因为发电机没有长度.
有没有办法做到这一点,没有定义计数功能?
def count(i):
c = 0
for el in i: c += 1
return c
Run Code Online (Sandbox Code Playgroud)
编辑:澄清,我明白整个文件必须阅读!我只是不想在内存中一次性=).
在今天的Boost图书馆会议上,"现代C++设计"和Loki C++库的作者Andrei Alexandrescu发表了题为"Iterators Must Go"(视频,幻灯片)的演讲,讲述了为什么迭代器不好,他有一个更好的解决方案.
我试着阅读演示幻灯片,但我无法从中得到很多.
我看到很多c ++代码看起来像这样:
for( const_iterator it = list.begin(),
const_iterator ite = list.end();
it != ite; ++it)
Run Code Online (Sandbox Code Playgroud)
与更简洁的版本相反:
for( const_iterator it = list.begin();
it != list.end(); ++it)
Run Code Online (Sandbox Code Playgroud)
这两个约定之间的速度会有什么不同吗?由于list.end()只被调用一次,因此第一个会稍快一些.但由于迭代器是const,似乎编译器会将此测试从循环中拉出来,为两者生成等效的汇编.
c++ compiler-construction optimization iterator coding-style
我有一个叫做的类Action,它实际上是一个围绕Move对象deque的包装器.
因为我需要遍历Moves前向和后向的双端队列,所以我有一个前向迭代器和一个reverse_iterator作为类的成员变量.这样做的原因是因为当我前往或后退时,我需要知道何时离开了双端队的"终点".
这个类看起来像这样:
class Action
{
public:
SetMoves(std::deque<Move> & dmoves) { _moves = dmoves; }
void Advance();
bool Finished()
{
if( bForward )
return (currentfwd==_moves.end());
else
return (currentbck==_moves.rend());
}
private:
std::deque<Move> _moves;
std::deque<Move>::const_iterator currentfwd;
std::deque<Move>::const_reverse_iterator currentbck;
bool bForward;
};
Run Code Online (Sandbox Code Playgroud)
该Advance功能如下:
void Action::Advance
{
if( bForward)
currentfwd++;
else
currentbck++;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我希望能够检索当前Move对象的迭代器,而无需查询我是向前还是向后.这意味着一个函数返回一种类型的迭代器,但我有两种类型.
我应该忘记返回一个迭代器,并返回一个Move对象的const引用 吗?
最好的祝愿,
BeeBand
C++ 98有front_inserter,back_inserter和inserter,但在C++ 11或草案C++ 14中似乎没有任何这些版本.有没有我们不能有任何技术原因front_emplacer,back_emplacer和emplacer?
目前,在JavaScript中处理一系列异步结果的唯一稳定方法是使用事件系统.但是,正在开发三种替代方案:
流:https
:
//streams.spec.whatwg.org Observables:https:
//tc39.github.io/proposal-observable Async Iterators:https://tc39.github.io/proposal-async-iteration
每个事件和其他事件的差异和好处是什么?
这些中的任何一个是否打算取代事件?
iterator ×10
c++ ×4
python ×3
.net ×1
c# ×1
c++11 ×1
c++14 ×1
coding-style ×1
dom-events ×1
ienumerable ×1
list ×1
observable ×1
optimization ×1
python-3.x ×1
range ×1
scala ×1
stl ×1
stream ×1