这两个接口之间的确切区别是什么?是否Enumeration有过使用效益Iterator?如果有人可以详细说明,那么参考文章将不胜感激.
我可以在Python中重置迭代器/生成器吗?我正在使用DictReader并希望将其重置(从csv模块)到文件的开头.
我有一个像这样定义的多行字符串:
foo = """
this is
a multi-line string.
"""
Run Code Online (Sandbox Code Playgroud)
这个字符串我们用作我正在编写的解析器的测试输入.解析器函数接收一个file-object作为输入并迭代它.它也next()直接调用方法来跳过行,所以我真的需要一个迭代器作为输入,而不是迭代.我需要一个迭代器,迭代遍历该字符串的各个行,就像file一个文本文件的行一样.我当然可以这样做:
lineiterator = iter(foo.splitlines())
Run Code Online (Sandbox Code Playgroud)
有更直接的方法吗?在这种情况下,字符串必须遍历一次以进行拆分,然后再由解析器遍历.在我的测试用例中没关系,因为那里的字符串很短,我只是出于好奇而问.Python为这些东西提供了许多有用且高效的内置插件,但我找不到任何适合这种需求的东西.
在Scala中,您经常使用迭代器以for递增的顺序执行循环,如:
for(i <- 1 to 10){ code }
Run Code Online (Sandbox Code Playgroud)
你会怎么做,所以它从10变为1?我想10 to 1给出一个空的迭代器(就像通常的范围数学)?
我制作了一个Scala脚本,它通过在迭代器上调用reverse来解决它,但是在我看来它不是很好,下面的方法是什么?
def nBeers(n:Int) = n match {
case 0 => ("No more bottles of beer on the wall, no more bottles of beer." +
"\nGo to the store and buy some more, " +
"99 bottles of beer on the wall.\n")
case _ => (n + " bottles of beer on the wall, " + n +
" bottles of beer.\n" +
"Take one down …Run Code Online (Sandbox Code Playgroud) 是否存在可以产生无限元素的直接生成器表达式?
这是一个纯粹的理论问题.这里不需要"实用"的答案:)
例如,很容易制作一个有限的发电机:
my_gen = (0 for i in xrange(42))
Run Code Online (Sandbox Code Playgroud)
但是,要创建一个无限的,我需要使用伪造的函数"污染"我的命名空间:
def _my_gen():
while True:
yield 0
my_gen = _my_gen()
Run Code Online (Sandbox Code Playgroud)
在单独的文件中执行操作并在import以后执行操作不计算在内.
我也知道这就是itertools.repeat这个.我很好奇是否有一个没有它的单线解决方案.
在Python 2.6中从迭代器获取最后一项的最佳方法是什么?例如,说
my_iter = iter(range(5))
Run Code Online (Sandbox Code Playgroud)
什么是最短的代码/最干净的方式4来自my_iter?
我可以做到这一点,但它看起来效率不高:
[x for x in my_iter][-1]
Run Code Online (Sandbox Code Playgroud) 现在,我有一个包含一段代码的程序,如下所示:
while (arrayList.iterator().hasNext()) {
//value is equal to a String value
if( arrayList.iterator().next().equals(value)) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
我是否正确,只要迭代ArrayList就可以了?
我得到的错误是:
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(Unknown Source)
at main1.endElement(main1.java:244)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at main1.traverse(main1.java:73)
at main1.traverse(main1.java:102)
at main1.traverse(main1.java:102)
at main1.main(main1.java:404)
Run Code Online (Sandbox Code Playgroud)
我会显示其余的代码,但它非常广泛,如果我没有正确地进行迭代,我会假设唯一的可能性是我没有ArrayList正确初始化.
你能想出一个很好的方法(可能用itertools)将迭代器拆分成给定大小的块吗?
因此l=[1,2,3,4,5,6,7]与chunks(l,3)变成一个迭代[1,2,3], [4,5,6], [7]
我可以想到一个小程序来做这个,但不是一个很好的方式可能itertools.
可能重复:
为什么使用迭代器而不是数组索引?
我正在回顾我对C++的了解,我偶然发现了迭代器.我想知道的一件事是什么让它们如此特别,我想知道为什么:
using namespace std;
vector<int> myIntVector;
vector<int>::iterator myIntVectorIterator;
// Add some elements to myIntVector
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);
for(myIntVectorIterator = myIntVector.begin();
myIntVectorIterator != myIntVector.end();
myIntVectorIterator++)
{
cout<<*myIntVectorIterator<<" ";
//Should output 1 4 8
}
Run Code Online (Sandbox Code Playgroud)
比这更好:
using namespace std;
vector<int> myIntVector;
// Add some elements to myIntVector
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);
for(int y=0; y<myIntVector.size(); y++)
{
cout<<myIntVector[y]<<" ";
//Should output 1 4 8
}
Run Code Online (Sandbox Code Playgroud)
是的,我知道我不应该使用std命名空间.我刚把这个例子从cprogramming网站上删除了.那么请你告诉我为什么后者更糟?有什么大不同?
iterator ×10
python ×6
generator ×2
java ×2
loops ×2
arraylist ×1
c++ ×1
c++11 ×1
collections ×1
enumeration ×1
for-loop ×1
indexing ×1
python-2.7 ×1
python-3.x ×1
scala ×1
string ×1