标签: iterator

加入一组有序整数的Python迭代器

这是一个看似简单的问题:给定一个按升序生成整数序列的迭代器列表,编写一个简洁的生成器,只生成每个序列中出现的整数.

在昨晚阅读了几篇论文之后,我决定在Python中破解一个完全最小的全文索引器,如此处所示(尽管该版本现在已经很老了).

我的问题在于search()函数,它必须迭代每个发布列表并仅产生每个列表上显示的文档ID.正如您从上面的链接中看到的那样,我当前的非递归"工作"尝试非常糟糕.

示例:

postings = [[1,   100, 142, 322, 12312],
            [2,   100, 101, 322, 1221],
            [100, 142, 322, 956, 1222]]
Run Code Online (Sandbox Code Playgroud)

应该产量:

[100, 322]
Run Code Online (Sandbox Code Playgroud)

至少有一个优雅的递归函数解决方案,但我想尽可能避免这种情况.但是,一个涉及嵌套生成器表达式,itertools滥用或任何其他类型的代码高尔夫的解决方案非常受欢迎.:-)

应该可以安排函数只需要与最小列表中的项目一样多的步骤,并且不将整个整数集吸入内存.将来,这些列表可能从磁盘读取,并且大于可用RAM.

在过去的30分钟里,我对我的舌尖有了一个想法,但我无法将其纳入代码中.请记住,这只是为了好玩!

python iterator join code-golf generator

17
推荐指数
3
解决办法
2094
查看次数

Scala Iterator(又名Iterator.init)中的最后一项除外

我有许多大文件,我想处理除了每个文件中的最后一行以外的所有文件.如果文件很小,我可以转换为TraversableLike并使用"init"方法,例如:

lines.toList.init
Run Code Online (Sandbox Code Playgroud)

但是文件很大所以我需要将事物保存为迭代器.有没有一种简单的方法可以在迭代器上获得类似"init"的内容?我正在考虑以下内容,但我不相信它会一直有效:

lines.takeWhile(_ => lines.hasNext)
Run Code Online (Sandbox Code Playgroud)

iterator scala

17
推荐指数
2
解决办法
7342
查看次数

Python中的迭代器(iter())函数.

对于字典,我可以iter()用来迭代字典的键.

y = {"x":10, "y":20}
for val in iter(y):
    print val
Run Code Online (Sandbox Code Playgroud)

当我有迭代器如下,

class Counter:
    def __init__(self, low, high):
        self.current = low
        self.high = high

    def __iter__(self):
        return self

    def next(self):
        if self.current > self.high:
            raise StopIteration
        else:
            self.current += 1
            return self.current - 1
Run Code Online (Sandbox Code Playgroud)

为什么我不能这样使用它

x = Counter(3,8)
for i in x:
    print x
Run Code Online (Sandbox Code Playgroud)

也不

x = Counter(3,8)
for i in iter(x):
    print x
Run Code Online (Sandbox Code Playgroud)

但这样呢?

for c in Counter(3, 8):
    print c
Run Code Online (Sandbox Code Playgroud)

功能的用途是iter()什么?

添加

我想这可能是如何iter() …

python iterator

17
推荐指数
2
解决办法
4万
查看次数

C++:如何用STL逐行遍历std :: string中的文本?

我在std :: string对象中有一个文本.该文由几行组成.我想使用STL(或Boost)逐行迭代文本.我提出的所有解决方案似乎都不是很优雅.我最好的方法是在换行符处拆分文本.有更优雅的解决方案吗?

更新:这就是我要找的:

std::string input;
// get input ...
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
  std::cout << line << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我以为我已经尝试过了.我收到了一个编译错误并把它丢了.快点!

c++ string boost iterator stl

17
推荐指数
2
解决办法
1万
查看次数

为什么我们使用entrySet()方法并使用返回的集来迭代映射?

通常我们写这个来从地图中获取键和值.

Map m=new HashMap();
Set s=map.entrySet();
Iterator i=s.iterator()
while(s.hasNext()){
    Map.Entry m= (map.Entry) s.next();
    System.out.println(""+m.getKey()+""+ m.getValue());
}
Run Code Online (Sandbox Code Playgroud)

为什么我们使用set迭代为什么不直接映射?

java collections dictionary iterator

17
推荐指数
2
解决办法
6万
查看次数

从迭代器返回对象的引用

我想从向量返回一个对象的引用,该对象在一个迭代器对象中.我怎样才能做到这一点?

我尝试了以下方法:

Customer& CustomerDB::getCustomerById (const string& id) {
    vector<Customer>::iterator i;
    for (i = customerList.begin(); i != customerList.end() && !(i->getId() == id); ++i);

    if (i != customerList.end())
        return *i; // is this correct?
    else
        return 0; // getting error here, cant return 0 as reference they say
}
Run Code Online (Sandbox Code Playgroud)

在代码中,customerList是客户的向量,函数getId返回客户的id.

是对的*i吗?我怎么能返回0或null作为参考?

c++ null iterator reference vector

17
推荐指数
1
解决办法
3万
查看次数

如何在迭代时从无序地图中删除多个项目?

请考虑以下情况:

using namespace std;
unordered_map<int, vector<A>> elements;
Run Code Online (Sandbox Code Playgroud)

现在我正在迭代这个无序的地图:

for (auto it = elements.begin(); it != elements.end(); ++it)
Run Code Online (Sandbox Code Playgroud)

在循环内部,我正在形成几个元素elements(当前的一个it指向和更多的元素,不一定是那些在线的那些!).因为每个元素只能是一个集群的一部分,所以我想从地图中删除它们,然后继续下一个元素(即构建下一个集群).

我怎么能这样做并仍然在正确的位置继续迭代?

c++ iterator unordered-map

17
推荐指数
3
解决办法
1万
查看次数

如何在循环中获取当前迭代器项的索引?

如何在循环中获取Python 迭代器的当前项的索引?

例如,当使用finditer返回迭代器的正则表达式函数时,如何在循环中访问迭代器的索引.

for item in re.finditer(pattern, text):
    # How to obtain the index of the "item"
Run Code Online (Sandbox Code Playgroud)

python iterator

17
推荐指数
1
解决办法
2万
查看次数

为什么可迭代对象不是迭代器?

这是我的代码:

from collections import deque

class linehistory:
    def __init__(self, lines, histlen=3):
        self.lines = lines
        self.history = deque(maxlen=histlen)

    def __iter__(self):
        for lineno, line in enumerate(self.lines,1):
            self.history.append((lineno, line))
            yield line

    def clear(self):
        self.history.clear()


f = open('somefile.txt')
lines = linehistory(f)
next(lines)
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
    TypeError: 'linehistory' object is not an iterator
Run Code Online (Sandbox Code Playgroud)

我不知道为什么linehistory对象不是迭代器,因为它已经__iter__the类中包含了方法.

python iterator

17
推荐指数
3
解决办法
1万
查看次数

为什么const_iterator可以与std :: map :: erase一起使用

我下的印象,一个不能使用eraseconst iterator.检查此代码.
为什么以下代码编译(C++ 11,gcc)?

long getMax(const bool get_new)
{
    long max_val=0;

    TO now=getNow();

    map<TO, long>& m=get_new?m_new:m_old;

    for(auto it=m.cbegin(); it !=m.cend())
    {
        if(now.compareTime((*it).first)<lookback)
        {
            max_val=max(max_val,
                        (*it).second);
            ++it;
        }
        else
        {
            it=m.erase(it);
        }
    }

    return max_val;
}
Run Code Online (Sandbox Code Playgroud)

地图本身不是恒定的,但我的理解是,const iterator应该使这一失败.

c++ iterator stdmap const-iterator c++11

17
推荐指数
2
解决办法
889
查看次数